DQL
DQL
Definition
DQL (Data Query Language) is the single-statement family — really just SELECT — used to read data back out of the database without changing anything. Some textbooks fold DQL into DML since SELECT doesn't modify data either way; treating it as its own category emphasizes that reading and writing data are conceptually different jobs.
How It Works
SELECT is the most-used, most expressive statement in SQL, built from the clauses covered in SQL Statement Structure:
sqlSELECT department, SUM(salary) DIV COUNT(*) AS avg_salary FROM employees WHERE department <> 'Contractors' GROUP BY department HAVING COUNT(*) >= 2 ORDER BY avg_salary DESC LIMIT 5;
This single statement filters rows (WHERE), groups them (GROUP BY), computes an aggregate per group (SUM/COUNT), filters groups (HAVING), sorts the result (ORDER BY), and caps how many rows come back (LIMIT) — all without ever touching the underlying data. Running the same SELECT a thousand times produces a thousand identical reads and zero side effects, a property called being read-only or idempotent in the safest sense.
Edge Cases and Pitfalls
SELECT *(all columns) is convenient while exploring data interactively, but is considered poor practice in application code: it silently returns extra columns (and extra network/memory cost) if the table gains new columns later, and it breaks if a queried column is ever renamed or removed. Naming exact columns is the safer, intention-revealing habit.- A
SELECTwith aJOINthat has an unintended many-to-many relationship can silently multiply rows (a form of "fan-out"), producing a result that looks plausible but double-counts or over-counts values — this is one of the most common sources of subtly wrong aggregate results. LIMITwithout a correspondingORDER BYreturns some n rows, but which n rows is not guaranteed to be stable across runs —LIMITonly produces a deterministic, meaningful result when paired with anORDER BYthat fully determines row order.
Key Takeaways
- DQL is fundamentally
SELECT— a read-only statement that never modifies data, however elaborate its clauses become. - Prefer explicit column lists over
SELECT *in real application code. LIMITneedsORDER BYto be meaningful and reproducible; otherwise "the first N rows" is an arbitrary, unstable choice made by the engine.