CompTIA Tech+ (FC0-U71)Data and Database FundamentalsMedium

A technician runs a SQL query to list all employees from an Employees table but wants the results displayed alphabetically by last name. Which clause should be added to the SELECT statement to achieve this?

  1. AGROUP BY LastName
  2. BHAVING LastName
  3. CWHERE LastName
  4. DORDER BY LastName
Show answer & explanation

Correct answer: D. ORDER BY LastName

The ORDER BY clause sorts the result set by one or more columns, and by default sorts in ascending (alphabetical) order. A query such as SELECT * FROM Employees ORDER BY LastName would list employees alphabetically by last name.

Why the other options are wrong

  • A. GROUP BY is used to aggregate rows sharing a common value, not to sort output.
  • B. HAVING filters grouped results after aggregation, it is not used for sorting.
  • C. WHERE filters which rows are returned based on a condition, it does not control sort order.

SQL ORDER BY Clause

A SQL clause used to sort the result set of a query by one or more columns, ascending or descending.

  • Default sort order is ascending (ASC) unless DESC is specified.
  • Can sort by multiple columns, listed in priority order.
  • Placed at the end of a SELECT statement, after WHERE if present.

Memory trick: ORDER BY lines up rows like students in alphabetical roll call.

More Data and Database Fundamentals questions