Syntax: Querying
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.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT * FROM t ORDER BY id LIMIT 10; |
| MySQL | SELECT * FROM t ORDER BY id LIMIT 10; |
| SQL Server | SELECT TOP (10) * FROM t ORDER BY id; |
| Oracle | SELECT * FROM t ORDER BY id FETCH FIRST 10 ROWS ONLY; |
| SQLite | SELECT * FROM t ORDER BY id LIMIT 10; |
Skip 20 Rows, Take 10
Standard. Lesson: Pagination.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT * FROM t ORDER BY id OFFSET 20 LIMIT 10; |
| MySQL | SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20; |
| SQL Server | SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY; |
| Oracle | SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY; |
| SQLite | SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20; |
Case-Insensitive Match
Dialect. Lesson: LIKE and Wildcards.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT * FROM t WHERE name ILIKE 'nav%'; |
| MySQL | SELECT * FROM t WHERE name LIKE 'nav%'; -- default collation ignores case |
| SQL Server | SELECT * FROM t WHERE name LIKE 'nav%'; -- default collation ignores case |
| Oracle | SELECT * FROM t WHERE LOWER(name) LIKE 'nav%'; |
| SQLite | SELECT * FROM t WHERE name LIKE 'nav%'; -- ignores ASCII case |
Rows with a Missing Value
Standard. Lesson: NULL Values.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT * FROM t WHERE email IS NULL; |
| MySQL | SELECT * FROM t WHERE email IS NULL; |
| SQL Server | SELECT * FROM t WHERE email IS NULL; |
| Oracle | SELECT * FROM t WHERE email IS NULL; |
| SQLite | SELECT * FROM t WHERE email IS NULL; |
Groups with a Condition
Standard. Lesson: HAVING.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1; |
| MySQL | SELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1; |
| SQL Server | SELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1; |
| Oracle | SELECT city, COUNT(*) FROM t GROUP BY city HAVING COUNT(*) > 1; |
| SQLite | SELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1; |
Full Outer Join
Standard. Lesson: FULL OUTER JOIN.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT * FROM a FULL JOIN b ON b.id = a.id; |
| MySQL | SELECT * FROM a LEFT JOIN b ON b.id = a.id UNION SELECT * FROM a RIGHT JOIN b ON b.id = a.id; |
| SQL Server | SELECT * FROM a FULL JOIN b ON b.id = a.id; |
| Oracle | SELECT * FROM a FULL JOIN b ON b.id = a.id; |
| SQLite | SELECT * FROM a FULL JOIN b ON b.id = a.id; -- 3.39+ |
Rows in One Table but Not Another
Standard. Lesson: INTERSECT and EXCEPT.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT id FROM a EXCEPT SELECT id FROM b; |
| MySQL | SELECT id FROM a EXCEPT SELECT id FROM b; -- 8.0.31+ |
| SQL Server | SELECT id FROM a EXCEPT SELECT id FROM b; |
| Oracle | SELECT id FROM a MINUS SELECT id FROM b; |
| SQLite | SELECT id FROM a EXCEPT SELECT id FROM b; |
Quote a Name with a Space
Standard. Lesson: SQL Syntax.
| Database | Syntax |
|---|---|
| PostgreSQL | SELECT "first name" FROM t; |
| MySQL | SELECT first name FROM t; |
| SQL Server | SELECT [first name] FROM t; |
| Oracle | SELECT "first name" FROM t; |
| SQLite | SELECT "first name" FROM t; |
Try the PostgreSQL Forms
These run on the Fee Tracker in 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; title | fee_usd
------------------+---------
Java Collections | 550.00
Java OOP | 500.00
(2 rows)
student_id
------------
102
(1 row)Was this tutorial helpful?