SQL Mastery: A Comprehensive Guide to Executing Common Excel Operations with SQL



Structured Query Language (SQL) is a powerful tool used to manage and manipulate data stored in relational databases. It is an essential skill for data analysts, data scientists, and anyone working with data, given its efficiency and versatility. One interesting aspect of SQL is its capability to perform tasks typically associated with spreadsheet software like Microsoft Excel. This comprehensive guide will walk you through how you can use SQL to execute common Excel operations, enhancing your data manipulation skills and optimizing your workflow.

Understanding SQL

SQL is a standard language for managing data held in a relational database management system (RDBMS) or for stream processing in a relational data stream management system (RDSMS). It is particularly useful for manipulating structured data, i.e., data incorporating relations among entities and variables.

SQL vs. Excel

While Excel is a powerful tool for data analysis and visualization, it can become sluggish and inefficient when handling large datasets. SQL, on the other hand, is designed to handle, manipulate, and query large datasets efficiently. Furthermore, SQL has superior capabilities for data manipulation and complex queries, making it a more robust tool for in-depth data analysis.

Performing Common Excel Tasks in SQL

Let’s explore how you can perform common Excel operations in SQL.

Sorting Data

Sorting data in Excel is a common operation, often done using the ‘Sort’ feature. In SQL, this operation is performed using the `ORDER BY` clause. The `ORDER BY` keyword sorts the records in ascending order by default. If you want to sort the records in descending order, you can use the `DESC` keyword.

For instance, if you have a table named ‘Sales’ with columns ‘Product’, ‘Quantity’, and ‘Price’, and you want to sort by ‘Price’ in descending order, your SQL command would look like this:


Filtering Data

In Excel, the ‘Filter’ feature allows you to display only the rows in a spreadsheet that meet specific criteria. The equivalent operation in SQL is performed using the `WHERE` clause. The `WHERE` clause is used to filter records and extract only those that fulfill a specified condition.

For example, to select only the sales records where the Quantity is greater than 10:

WHERE Quantity > 10;

Applying Mathematical Operations

Excel is often used to perform mathematical operations on data, such as finding the sum or average of a column of numbers. In SQL, these operations can be executed using aggregate functions.

For instance, to find the total quantity of products sold (sum of the ‘Quantity’ column), you would use the `SUM` function:

SELECT SUM(Quantity) FROM Sales;

To find the average sales price, you would use the `AVG` function:


Pivot Tables

Pivot tables in Excel are used to summarize, analyze, explore, and present summary data. In SQL, this can be achieved using a combination of the `GROUP BY` clause and aggregate functions.

For example, to find the total quantity sold of each product (equivalent to creating a pivot table in Excel with ‘Product’ as rows and the sum of ‘Quantity’ as values), you could use:

SELECT Product, SUM(Quantity) FROM Sales
GROUP BY Product;

Joining Tables

In Excel, you might use ‘VLOOKUP’ or ‘INDEX/MATCH’ to combine data from different tables based on a common column. In SQL, this operation is performed using the `JOIN` clause.

The most common types of `JOIN` operations in SQL are:

INNER JOIN: This returns records that have matching values in both tables.
LEFT (OUTER) JOIN: This returns all records from the left table and the matched records from the right table.
RIGHT (OUTER) JOIN: This returns all records from the right table and the matched records from the left table.
FULL (OUTER) JOIN: This returns all records when there is a match in either the left or the right table.

For instance, if you have another table ‘Products’ with columns ‘Product’ and ‘Category’, and you want to add the category information to the sales records, you would use an `INNER JOIN`:

SELECT Sales.Product, Sales.Quantity, Sales.Price, Products.Category 
FROM Sales
ON Sales.Product = Products.Product;


In Excel, you might use the ‘Subtotal’ function to calculate subtotals for different groups in your data. In SQL, this can be achieved using the `GROUP BY` clause along with aggregate functions.

For example, to calculate the total quantity sold for each product category:

SELECT Products.Category, SUM(Sales.Quantity)
FROM Sales
ON Sales.Product = Products.Product
GROUP BY Products.Category;

Data Transformation

There are several SQL functions that allow you to perform data transformations similar to those you might perform in Excel. For instance, you might use the `TRIM` function to remove leading and trailing spaces from a string, or the `SUBSTRING` function to extract a portion of a string. Date transformations can be done using SQL functions such as `YEAR`, `MONTH`, and `DAY`.


SQL is an incredibly versatile language for managing and manipulating data. As we’ve seen, it can perform many of the operations commonly executed in Excel, often more efficiently and with less manual effort. While Excel remains a valuable tool for certain tasks, particularly those involving small datasets and visual data exploration, SQL offers superior performance and flexibility for large datasets and complex queries.

Learning how to perform common Excel operations in SQL can greatly enhance your data manipulation skills, optimize your workflow, and enable you to handle larger and more complex data tasks. With practice, you’ll find that many tasks you’re accustomed to performing in Excel can be executed efficiently and effectively in SQL.

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included:Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Find more … …

ML Tutorials – What is Machine Learning?

R tutorials for Business Analyst – R Data Frame: Create, Append, Select, Subset

Excel formula for Beginners – How to SUMPRODUCT with IF in Excel