If you can filter, sort, use COUNTIF and run a VLOOKUP in Excel, you already know the logic of SQL. SQL also keeps data in tables of rows and columns, but you write each step as a short query instead of clicking. A filter becomes WHERE, sorting becomes ORDER BY, COUNTIF becomes GROUP BY and VLOOKUP becomes JOIN. You can save a query, share it and run it again on new data.
The examples use the student and department tables of the Northbridge University database from the SQL course. NULL means "no value in this cell", like an empty cell in Excel.
How do you select columns and filter rows in SQL?
In Excel you hide columns and use the filter button. In SQL you name the columns you want after SELECT, and you put the filter after WHERE. Students from Warsaw who are in year 3:
SELECT first_name, last_name, year FROM student WHERE city = 'Warsaw' AND year = 3 ORDER BY id;
| first_name | last_name | year |
|---|---|---|
| Ayesha | Singh | 3 |
| Maya | Hassan | 3 |
| Jonas | Popescu | 3 |
| Ethan | Bianchi | 3 |
Every query follows the same pattern: SELECT says which columns, FROM says which table, WHERE says which rows. A query never changes the table. It only returns the view you asked for.
How do you sort in SQL?
Excel's "Sort largest to smallest" is ORDER BY ... DESC:
SELECT first_name, last_name, gpa FROM student WHERE city = 'Warsaw' AND gpa IS NOT NULL ORDER BY gpa DESC;
| first_name | last_name | gpa |
|---|---|---|
| Kenji | Silva | 3.83 |
| Ethan | Bianchi | 3.69 |
| Diego | Rossi | 2.95 |
| Ayesha | Kowalski | 2.74 |
| Jonas | Popescu | 2.54 |
| Rosa | Haddad | 2.12 |
| Ayesha | Singh | 1.59 |
| Maya | Hassan | 0.92 |
The line gpa IS NOT NULL leaves out Kenji Lefèvre, a first-year student whose GPA is still empty. Without it, PostgreSQL puts the NULL value at the top of a descending sort, and the list starts with Kenji Lefèvre.
What is the SQL version of COUNTIF, SUMIF and AVERAGEIF?
Those functions, and pivot tables, become GROUP BY with COUNT, SUM or AVG:
SELECT year, COUNT(*) AS students, ROUND(AVG(gpa), 2) AS avg_gpa FROM student WHERE city = 'Warsaw' GROUP BY year ORDER BY year;
| year | students | avg_gpa |
|---|---|---|
| 1 | 1 | NULL |
| 2 | 3 | 3.17 |
| 3 | 4 | 2.19 |
| 4 | 1 | 2.12 |
Year 1 has one student, but the average is NULL: AVG skips empty values, and here there is nothing left to average. Excel's AVERAGE also ignores empty cells, and it shows #DIV/0! when no number is left.
How do you make a pivot table in SQL?
A pivot table with a count per category in the columns uses SUM(CASE WHEN ...). This one counts students by department and year:
SELECT d.name AS department, SUM(CASE WHEN s.year = 2 THEN 1 ELSE 0 END) AS year_2, SUM(CASE WHEN s.year = 3 THEN 1 ELSE 0 END) AS year_3, SUM(CASE WHEN s.year = 4 THEN 1 ELSE 0 END) AS year_4 FROM student s JOIN department d ON d.id = s.department_id GROUP BY d.name ORDER BY d.name;
| department | year_2 | year_3 | year_4 |
|---|---|---|---|
| Business Administration | 6 | 6 | 5 |
| Computer Programming | 8 | 0 | 0 |
| Industrial Engineering | 3 | 3 | 3 |
| Management Information Systems | 5 | 6 | 0 |
| New Media and Communication | 2 | 4 | 3 |
| Psychology | 4 | 9 | 6 |
| Software Engineering | 7 | 7 | 4 |
Each CASE WHEN works like an IF: count the row only when the year matches.
What is the SQL version of VLOOKUP?
VLOOKUP fetches a value from another table by a matching key. In SQL that is JOIN. Here the department name comes from the department table:
SELECT s.first_name, s.last_name, d.name AS department FROM student s JOIN department d ON d.id = s.department_id WHERE s.city = 'Warsaw' AND s.year = 3 ORDER BY s.id;
| first_name | last_name | department |
|---|---|---|
| Ayesha | Singh | Business Administration |
| Maya | Hassan | Business Administration |
| Jonas | Popescu | Psychology |
| Ethan | Bianchi | New Media and Communication |
Note what a plain JOIN does with a row that has no match: it drops it. Look at departments, joined the other way. The Data Science and Analytics department has no students yet. With LEFT JOIN it stays in the result, the way you would want a VLOOKUP to show #N/A instead of removing the row:
SELECT d.name AS department, COUNT(s.id) AS students FROM department d LEFT JOIN student s ON s.department_id = d.id GROUP BY d.name ORDER BY students, d.name;
| department | students |
|---|---|
| Data Science and Analytics | 0 |
| New Media and Communication | 10 |
| Industrial Engineering | 12 |
| Computer Programming | 14 |
| Management Information Systems | 17 |
| Software Engineering | 20 |
| Psychology | 23 |
| Business Administration | 24 |
More on joins is in the [SQL join types](/blog/sql-join-types) post.
What is the SQL version of the IF formula?
Use CASE WHEN:
SELECT first_name, last_name, gpa, CASE WHEN gpa >= 3 THEN 'honors' WHEN gpa IS NULL THEN 'no GPA yet' ELSE 'regular' END AS standing FROM student WHERE city = 'Warsaw' ORDER BY id;
| first_name | last_name | gpa | standing |
|---|---|---|---|
| Ayesha | Singh | 1.59 | regular |
| Maya | Hassan | 0.92 | regular |
| Kenji | Silva | 3.83 | honors |
| Kenji | Lefèvre | NULL | no GPA yet |
| Rosa | Haddad | 2.12 | regular |
| Ayesha | Kowalski | 2.74 | regular |
| Diego | Rossi | 2.95 | regular |
| Jonas | Popescu | 2.54 | regular |
| Ethan | Bianchi | 3.69 | honors |
Excel to SQL lookup table
| Excel | SQL |
|---|---|
| Hide columns | SELECT column1, column2 |
| Filter | WHERE |
| Sort | ORDER BY |
| COUNTIF, SUMIF, AVERAGEIF | COUNT, SUM, AVG with GROUP BY |
| PivotTable | GROUP BY, SUM(CASE WHEN ...) |
| VLOOKUP | JOIN |
| IF | CASE WHEN |
| Remove Duplicates | DISTINCT |
| Empty cell | NULL |
For example, SELECT COUNT(DISTINCT city) FROM student returns 12 distinct cities.
Why use SQL when you have Excel?
- Repeatable: save the query and run it again with new data next month.
- Large data: an Excel sheet holds at most 1,048,576 rows and slows down well before that. Databases are built for millions of rows.
- One source: everyone reads the same data, and file copies stop multiplying.
- Transparent: you can read how a result was found in the query text.
Common mistakes
- Text without quotes:
WHERE city = Warsawfails withcolumn "warsaw" does not exist. Write'Warsaw'. - Comparing NULL with `=`:
WHERE gpa = NULLruns without error but returns 0 matches. Writegpa IS NULL, which finds 32 students. - Thinking in cells: there is no "cell B3" in SQL. You work on all rows that match a condition.
- Trusting the order: without
ORDER BY, the order of rows is not guaranteed.
What to read next
- [SQL course](/courses/sql): learn these steps with exercises that run in your browser. Start with [selecting columns](/learn/sql/select-columns).
- [SQL join types](/blog/sql-join-types): the full picture for the VLOOKUP part.
- [Python or SQL first?](/blog/python-vs-sql-which-first)
- #beginner
- #sql
- #excel
- #tables