Skip to content
Hands-on Mentor
ENTR
Back to blog

SQL for Excel users: the SQL version of filters, VLOOKUP and pivot tables

You already know the logic of SQL if you use filters, COUNTIF and VLOOKUP in Excel. See each Excel task as a short SQL query, plus a lookup table.

6 min read

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_namelast_nameyear
AyeshaSingh3
MayaHassan3
JonasPopescu3
EthanBianchi3

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_namelast_namegpa
KenjiSilva3.83
EthanBianchi3.69
DiegoRossi2.95
AyeshaKowalski2.74
JonasPopescu2.54
RosaHaddad2.12
AyeshaSingh1.59
MayaHassan0.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;
yearstudentsavg_gpa
11NULL
233.17
342.19
412.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;
departmentyear_2year_3year_4
Business Administration665
Computer Programming800
Industrial Engineering333
Management Information Systems560
New Media and Communication243
Psychology496
Software Engineering774

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_namelast_namedepartment
AyeshaSinghBusiness Administration
MayaHassanBusiness Administration
JonasPopescuPsychology
EthanBianchiNew 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;
departmentstudents
Data Science and Analytics0
New Media and Communication10
Industrial Engineering12
Computer Programming14
Management Information Systems17
Software Engineering20
Psychology23
Business Administration24

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_namelast_namegpastanding
AyeshaSingh1.59regular
MayaHassan0.92regular
KenjiSilva3.83honors
KenjiLefèvreNULLno GPA yet
RosaHaddad2.12regular
AyeshaKowalski2.74regular
DiegoRossi2.95regular
JonasPopescu2.54regular
EthanBianchi3.69honors

Excel to SQL lookup table

ExcelSQL
Hide columnsSELECT column1, column2
FilterWHERE
SortORDER BY
COUNTIF, SUMIF, AVERAGEIFCOUNT, SUM, AVG with GROUP BY
PivotTableGROUP BY, SUM(CASE WHEN ...)
VLOOKUPJOIN
IFCASE WHEN
Remove DuplicatesDISTINCT
Empty cellNULL

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 = Warsaw fails with column "warsaw" does not exist. Write 'Warsaw'.
  • Comparing NULL with `=`: WHERE gpa = NULL runs without error but returns 0 matches. Write gpa 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
All posts