Databases · SQL Operations
SELECT and Filtering
Cheat sheet · https://pcornel.dk/topic.php?topic=6164
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 topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -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