DATABASES · 16 MIN READ

Selecting and filtering rows with SQL

A SELECT query names output columns, a source table, and optional conditions that limit or order the result.

Learning objectives

  • Read SELECT, FROM, WHERE, and ORDER BY.
  • Use a parameter rather than string-concatenating untrusted input in application code.

Read the query

SQL
SELECT name, score
FROM students
WHERE department = 'CSE'
ORDER BY score DESC;

FROM identifies the rows to consider. WHERE retains rows whose department equals CSE. SELECT chooses which columns appear in the result. ORDER BY orders those result rows by score descending.

Common mistakes

  • Use WHERE before assuming a query affects only intended rows.
  • In a real application, pass values as query parameters supported by the database driver; never build SQL by joining raw user input into a string.
  • Database products differ in extensions and details; consult the target product’s documentation.

References