SQL quiz

SELECT, joins, grouping, constraints, transactions and window functions.

65 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 65 questions with answers
  1. Which SQL statement retrieves data from a table?

    • GET
    • FETCH
    • SELECT (correct)
    • READ
  2. Which clause filters rows in a SELECT query?

    • ORDER BY
    • WHERE (correct)
    • FROM
    • GROUP BY
  3. Which clause sorts the result of a query?

    • ORDER BY (correct)
    • GROUP BY
    • SORT BY
    • ARRANGE
  4. Which keyword sorts results from highest to lowest?

    • REVERSE
    • DESC (correct)
    • ASC
    • DOWN
  5. Which SQL statement adds a new row to a table?

    • UPDATE
    • INSERT INTO (correct)
    • CREATE ROW
    • ADD ROW
  6. Which SQL statement changes existing rows?

    • CHANGE
    • MODIFY
    • UPDATE (correct)
    • ALTER ROW
  7. Which SQL statement removes rows from a table?

    • DELETE (correct)
    • ERASE
    • REMOVE
    • DROP ROW
  8. What does SELECT * return?

    • All tables
    • Only the first column
    • Only the primary key
    • All columns (correct)
  9. Which function counts rows?

    • SUM()
    • TOTAL()
    • NUMBER()
    • COUNT() (correct)
  10. Which function returns the average of a column?

    • AVERAGE()
    • MEAN()
    • AVG() (correct)
    • MID()
  11. Which function returns the largest value in a column?

    • MAX() (correct)
    • TOP()
    • BIG()
    • HIGHEST()
  12. Which keyword removes duplicate rows from a result?

    • DISTINCT (correct)
    • UNIQUE
    • DIFFERENT
    • SINGLE
  13. Which operator checks for a value within a range?

    • WITHIN
    • BETWEEN (correct)
    • RANGE
    • INSIDE
  14. Which operator matches a pattern such as names starting with A?

    • MATCH
    • LIKE (correct)
    • SIMILAR
    • PATTERN
  15. In LIKE patterns, what does % match?

    • Any sequence of zero or more characters (correct)
    • Only spaces
    • Exactly one character
    • Only digits
  16. How do you check if a column has no value?

    • = NULL
    • == NULL
    • IS EMPTY
    • IS NULL (correct)
  17. Which keyword limits the number of rows returned in PostgreSQL and MySQL?

    • TOP 10 only
    • MAXROWS
    • FIRST
    • LIMIT (correct)
  18. Which statement creates a new table?

    • ADD TABLE
    • MAKE TABLE
    • CREATE TABLE (correct)
    • NEW TABLE
  19. Which statement deletes a whole table, including its structure?

    • DROP TABLE (correct)
    • DELETE TABLE
    • CLEAR TABLE
    • REMOVE TABLE
  20. What is a row in a table also called?

    • A key
    • A record (correct)
    • A schema
    • A field
  21. Which clause groups rows that share a value so aggregates can be computed per group?

    • GROUP BY (correct)
    • ORDER BY
    • CLUSTER
    • PARTITION
  22. Which clause filters groups after aggregation?

    • FILTER
    • LIMIT
    • WHERE
    • HAVING (correct)
  23. What is the difference between WHERE and HAVING?

    • HAVING runs first
    • WHERE filters rows before grouping; HAVING filters groups after (correct)
    • WHERE works only with aggregates
    • They are identical
  24. Which join returns all rows from the left table and matching rows from the right?

    • LEFT JOIN (correct)
    • CROSS JOIN
    • INNER JOIN
    • RIGHT JOIN only
  25. What does an INNER JOIN return?

    • Only rows with matching values in both tables (correct)
    • The Cartesian product
    • All rows from both tables
    • All rows from the left table
  26. What does a CROSS JOIN produce?

    • Only unique rows
    • Rows that do not match
    • Only matching rows
    • Every combination of rows from both tables (correct)
  27. What does a FULL OUTER JOIN return?

    • Only unmatched rows
    • Only rows from the right table
    • Only the first match
    • All rows from both tables, matched where possible (correct)
  28. What is a primary key?

    • A column or set of columns that uniquely identifies each row (correct)
    • A key used for encryption
    • The first column of a table
    • A column that allows duplicates
  29. What is a foreign key?

    • A unique index
    • A column that references the primary key of another table (correct)
    • A column with only NULL values
    • A key from another database server
  30. What does the UNIQUE constraint do?

    • Prevents duplicate values in a column (correct)
    • Encrypts the column
    • Makes the column the primary key
    • Sorts the column
  31. What does the NOT NULL constraint do?

    • Sets a default of 0
    • Requires a value in every row for that column (correct)
    • Makes the column optional
    • Removes NULL rows
  32. What does the DEFAULT constraint do?

    • Prevents updates
    • Supplies a value when none is given on insert (correct)
    • Sorts new rows first
    • Sets the primary key
  33. What is the result of COUNT(column) when some values are NULL?

    • It returns NULL
    • It counts all rows
    • It returns an error
    • It counts only non-NULL values (correct)
  34. Which statement adds a new column to an existing table?

    • ALTER TABLE ... ADD COLUMN (correct)
    • UPDATE TABLE ... ADD
    • MODIFY TABLE ... NEW
    • INSERT COLUMN
  35. What does UNION do?

    • Joins two tables by key
    • Combines results and keeps duplicates
    • Intersects two results
    • Combines results of two queries and removes duplicates (correct)
  36. How does UNION ALL differ from UNION?

    • UNION ALL removes duplicates
    • UNION ALL sorts results
    • They are identical
    • UNION ALL keeps duplicate rows (correct)
  37. What is a subquery?

    • A query nested inside another query (correct)
    • A stored procedure
    • A query on a sub-table
    • A query with LIMIT
  38. What does IN (1, 2, 3) check?

    • Whether a list has 3 items
    • Whether a value is between 1 and 3
    • Whether the column is indexed
    • Whether a value matches any value in the list (correct)
  39. What does an index do?

    • Speeds up lookups at the cost of extra storage and slower writes (correct)
    • Encrypts a column
    • Prevents duplicates always
    • Sorts the table permanently
  40. What does COALESCE(a, b, c) return?

    • The first non-NULL value (correct)
    • The last value
    • NULL if any value is NULL
    • The sum of the values
  41. What is a view?

    • A saved query that can be used like a table (correct)
    • A copy of a table
    • An index
    • A backup
  42. Which clause skips a number of rows before returning results?

    • AFTER
    • JUMP
    • OFFSET (correct)
    • SKIP
  43. What does the CASE expression do?

    • Creates a table
    • Returns values based on conditions, like if/else (correct)
    • Starts a transaction
    • Changes text case
  44. In SQL, what is an alias?

    • A temporary name given to a table or column using AS (correct)
    • An index name
    • A copy of a table
    • A user role
  45. Which aggregate function ignores NULL values when adding?

    • SUM() (correct)
    • CONCAT()
    • COUNT(*)
    • ROW_NUMBER()
  46. What is a transaction?

    • A single SELECT
    • A type of index
    • A group of statements that succeed or fail as one unit (correct)
    • A backup
  47. Which command saves all changes in the current transaction?

    • COMMIT (correct)
    • SAVE
    • ROLLBACK
    • END CHANGES
  48. Which command undoes the changes of the current transaction?

    • ROLLBACK (correct)
    • REVERT
    • UNDO
    • CANCEL
  49. What does ACID stand for?

    • Access, Control, Index, Database
    • Atomic, Concurrent, Indexed, Durable
    • Accuracy, Completeness, Integrity, Data
    • Atomicity, Consistency, Isolation, Durability (correct)
  50. What does a window function like ROW_NUMBER() OVER (ORDER BY score DESC) do?

    • Deletes duplicate rows
    • Numbers rows in order without collapsing them into groups (correct)
    • Creates a view
    • Groups rows into one
  51. What is the difference between RANK() and DENSE_RANK()?

    • They are identical
    • RANK ignores ties
    • RANK leaves gaps after ties; DENSE_RANK does not (correct)
    • DENSE_RANK leaves gaps
  52. What does PARTITION BY do in a window function?

    • Splits rows into groups that the function runs over separately (correct)
    • Splits a table across disks
    • Sorts rows globally
    • Removes rows
  53. What is a correlated subquery?

    • A subquery that returns one value only
    • A subquery that refers to columns of the outer query and runs per row (correct)
    • A subquery with a JOIN
    • A subquery in the FROM clause
  54. What is a CTE, written with WITH?

    • A stored function
    • A named temporary result set used within a single query (correct)
    • A trigger
    • A permanent table
  55. What does a recursive CTE let you do?

    • Query hierarchical data such as trees by repeatedly joining to itself (correct)
    • Delete rows recursively
    • Create tables in a loop
    • Run queries in parallel
  56. Which isolation level prevents dirty reads but allows non-repeatable reads?

    • Repeatable Read only
    • Read Uncommitted
    • Serializable
    • Read Committed (correct)
  57. What is a dirty read?

    • Reading from a backup
    • Reading data without an index
    • Reading deleted data
    • Reading data written by a transaction that has not committed (correct)
  58. What does EXPLAIN show?

    • Query results
    • The execution plan the database will use for a query (correct)
    • The table structure
    • Errors in the query
  59. What does ON DELETE CASCADE do on a foreign key?

    • Prevents deleting the parent
    • Deletes child rows automatically when the parent row is deleted (correct)
    • Sets the child column to NULL
    • Deletes the whole table
  60. What is the difference between DELETE and TRUNCATE?

    • They are identical
    • TRUNCATE quickly removes all rows without filtering; DELETE can remove selected rows (correct)
    • DELETE drops the table
    • TRUNCATE can use WHERE
  61. What is SQL injection?

    • A way to add rows quickly
    • An attack that inserts malicious SQL through user input (correct)
    • A database backup method
    • An indexing technique
  62. What is the best defence against SQL injection?

    • Hiding error messages
    • Longer table names
    • Using uppercase SQL
    • Parameterized queries (correct)
  63. What does EXISTS (subquery) return?

    • True if the subquery returns at least one row (correct)
    • The first row
    • True only if every row matches
    • The number of rows
  64. In SQL, what is NULL = NULL?

    • Unknown (not true) (correct)
    • True
    • An error
    • False only in MySQL
  65. What does a composite primary key consist of?

    • A key with text and numbers
    • A primary key and a foreign key
    • Two or more columns that together identify a row (correct)
    • A hashed key