Syntax: Querying

Updated · 4 min read

This page lists common querying tasks in five databases, so you can copy the form your database understands. Each task shows whether the SQL standard defines it (standard) or each database has its own way (dialect). For what each feature does and when to use it, follow the lesson link under each task. The SQL Syntax by Database page explains how to find out which database and version you're using.

How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.

First 10 Rows

Standard. Lesson: Limiting Rows: LIMIT, TOP and FETCH.

DatabaseSyntax
PostgreSQLSELECT * FROM t ORDER BY id LIMIT 10;
MySQLSELECT * FROM t ORDER BY id LIMIT 10;
SQL ServerSELECT TOP (10) * FROM t ORDER BY id;
OracleSELECT * FROM t ORDER BY id FETCH FIRST 10 ROWS ONLY;
SQLiteSELECT * FROM t ORDER BY id LIMIT 10;

Skip 20 Rows, Take 10

Standard. Lesson: Pagination.

DatabaseSyntax
PostgreSQLSELECT * FROM t ORDER BY id OFFSET 20 LIMIT 10;
MySQLSELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20;
SQL ServerSELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
OracleSELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
SQLiteSELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20;

Case-Insensitive Match

Dialect. Lesson: LIKE and Wildcards.

DatabaseSyntax
PostgreSQLSELECT * FROM t WHERE name ILIKE 'nav%';
MySQLSELECT * FROM t WHERE name LIKE 'nav%'; -- default collation ignores case
SQL ServerSELECT * FROM t WHERE name LIKE 'nav%'; -- default collation ignores case
OracleSELECT * FROM t WHERE LOWER(name) LIKE 'nav%';
SQLiteSELECT * FROM t WHERE name LIKE 'nav%'; -- ignores ASCII case

Rows with a Missing Value

Standard. Lesson: NULL Values.

DatabaseSyntax
PostgreSQLSELECT * FROM t WHERE email IS NULL;
MySQLSELECT * FROM t WHERE email IS NULL;
SQL ServerSELECT * FROM t WHERE email IS NULL;
OracleSELECT * FROM t WHERE email IS NULL;
SQLiteSELECT * FROM t WHERE email IS NULL;

Groups with a Condition

Standard. Lesson: HAVING.

DatabaseSyntax
PostgreSQLSELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1;
MySQLSELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1;
SQL ServerSELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1;
OracleSELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1;
SQLiteSELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1;

Full Outer Join

Standard. Lesson: FULL OUTER JOIN.

DatabaseSyntax
PostgreSQLSELECT * FROM a FULL JOIN b ON b.id = a.id;
MySQLSELECT * FROM a LEFT JOIN b ON b.id = a.id UNION SELECT * FROM a RIGHT JOIN b ON b.id = a.id;
SQL ServerSELECT * FROM a FULL JOIN b ON b.id = a.id;
OracleSELECT * FROM a FULL JOIN b ON b.id = a.id;
SQLiteSELECT * FROM a FULL JOIN b ON b.id = a.id; -- 3.39+

Rows in One Table but Not Another

Standard. Lesson: INTERSECT and EXCEPT.

DatabaseSyntax
PostgreSQLSELECT id FROM a EXCEPT SELECT id FROM b;
MySQLSELECT id FROM a EXCEPT SELECT id FROM b; -- 8.0.31+
SQL ServerSELECT id FROM a EXCEPT SELECT id FROM b;
OracleSELECT id FROM a MINUS SELECT id FROM b;
SQLiteSELECT id FROM a EXCEPT SELECT id FROM b;

Quote a Name with a Space

Standard. Lesson: SQL Syntax.

DatabaseSyntax
PostgreSQLSELECT "first name" FROM t;
MySQLSELECT first name FROM t;
SQL ServerSELECT [first name] FROM t;
OracleSELECT "first name" FROM t;
SQLiteSELECT "first name" FROM t;

Try the PostgreSQL Forms

These run on the Fee Tracker in Try it:

SQL▶ Try it
SELECT title, fee_usd
FROM courses
WHERE title ILIKE 'java%'
ORDER BY fee_usd DESC, title
FETCH FIRST 2 ROWS ONLY;
 
SELECT student_id FROM enrollments
EXCEPT
SELECT student_id FROM payments
ORDER BY student_id;
Output
      title       | fee_usd
------------------+---------
 Java Collections |  550.00
 Java OOP         |  500.00
(2 rows)
 
 student_id
------------
        102
(1 row)

Was this tutorial helpful?