SQL Quiz
Questions: 16 · 10 minutes
1. Two compatible SELECT queries return email addresses. Which operator combines their results while removing duplicate rows?
JOIN
UNION
UNION ALL
INTERSECT
2. You need every customer, any matching orders, and no unmatched order-only rows. With customers on the left, which join fits?
CROSS JOIN
INNER JOIN
FULL OUTER JOIN
LEFT JOIN
3. During a transaction, an account is debited, but an error occurs before the matching credit and before commit. Which command should cancel the transaction's changes?
COMMIT
SAVEPOINT
ROLLBACK
GRANT
4. Which query returns employees whose salary is greater than the average salary across all employees?
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
SELECT * FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
SELECT * FROM employees WHERE salary > ALL (SELECT salary FROM employees);
5. A query must include customers who are active and whose country is either the US or Canada. Which filter expresses that requirement correctly?
Require active US customers, but include Canadian customers regardless of status
Require active status together with a parenthesized choice between US and Canada
Require each customer to have both US and Canada as the country value
Include anyone who is active, plus anyone located in either listed country
6. Which query returns only the name and email columns from the customers table?
FROM customers SELECT name, email;
SELECT * FROM customers;
SELECT name AND email FROM customers;
SELECT name, email FROM customers;
7. Which clause limits individual rows before GROUP BY creates groups?
HAVING
ORDER BY
WHERE
DISTINCT
8. What is the main role of a foreign key?
To make every value in its column unique
To replace the need for joins
To encrypt values shared between tables
To reference a candidate key in another or the same table and support referential integrity
9. Which expression calculates a running total of amount in date order while retaining each original row?
SUM(amount) GROUP BY order_date
SUM(amount) OVER ()
SUM(amount) OVER (ORDER BY order_date)
LAG(amount) OVER (ORDER BY order_date)
10. Which query correctly counts the number of orders for each customer?
SELECT DISTINCT customer_id, COUNT(*) FROM orders;
SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id;
SELECT customer_id, COUNT(*) FROM orders ORDER BY customer_id;
SELECT customer_id FROM orders HAVING COUNT(*);
11. A table has eight rows, and discount_code is NULL in three of them. What does COUNT(discount_code) return?
5
3
8
0
12. An application must look up a user by a value entered into a form. Which approach most directly reduces SQL injection risk?
Concatenate the value into the SQL string after removing spaces
Hide the SQL statement from the application's users
Escape only quotation marks and build the query manually
Use a parameterized query and bind the entered value as a parameter
13. A grouped query should return only departments with more than 10 employees. Which condition belongs after GROUP BY?
WHERE COUNT(*) > 10
HAVING COUNT(*) > 10
ORDER BY COUNT(*) > 10
DISTINCT COUNT(*) > 10
14. A report should return only records where middle_name has no stored value. Which predicate correctly checks for SQL NULL?
middle_name IS NULL
middle_name equals the text literal 'NULL'
middle_name = NULL
NULLIF(middle_name, '')
15. What is the effect of running UPDATE products SET price = price * 1.10; without a WHERE clause?
Only products with a NULL price are updated
The products are permanently sorted by price
The update is applied to every row in products
A new price column is created
16. What is the primary purpose of a primary key?
To uniquely identify each row in a table
To automatically sort every query result
To store references to rows in another table
To allow duplicate identifiers while improving search speed