Pcornel
Databases · SQL Operations

SELECT and Filtering

Cheat sheet · https://pcornel.dk/topic.php?topic=6164

Back to the Topic

Answer

SELECT retrieves rows: choose columns, FROM tables, filter with WHERE, sort with ORDER BY, and limit with LIMIT/TOP.

Remember

Select only needed columns. Filters on indexed columns without wrapping them in functions can use indexes. NULL needs IS NULL, not = NULL.

First checks

  • IS NULL — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

Where it usually breaks

WHERE col = NULL returns nothing.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6164

Confirmation

Confirm action

Continue?