DA SQL and Database Querying 1 — Questions and Answers
Question 1: Which SQL clause is used to filter rows after a GROUP BY operation?
- WHERE
- HAVING (Correct answer)
- ORDER BY
- FILTER
Correct answer: HAVING
HAVING is used to filter aggregated results produced by GROUP BY, whereas WHERE filters individual rows before aggregation.
Question 2: What does a LEFT JOIN return?
- Only matching rows from both tables
- All rows from the left table and matching rows from the right (Correct answer)
- Only rows unique to the left table
- All rows from both tables with no filtering
Correct answer: All rows from the left table and matching rows from the right
A LEFT JOIN returns all rows from the left table and the matched rows from the right table; unmatched right-table rows appear as NULL.
Question 3: Which aggregate function counts only non-NULL values in a column?
- COUNT(*)
- SUM()
- COUNT(column_name) (Correct answer)
- AVG()
Correct answer: COUNT(column_name)
COUNT(column_name) counts only non-NULL entries in that column, while COUNT(*) counts all rows including those with NULLs.
Question 4: What is the purpose of the SQL DISTINCT keyword?
- Sort results in ascending order
- Remove duplicate rows from the result set (Correct answer)
- Filter rows based on a condition
- Join two tables together
Correct answer: Remove duplicate rows from the result set
DISTINCT eliminates duplicate rows from query results, returning only unique value combinations.
Question 5: Which SQL statement is used to modify existing data in a table?
- INSERT
- ALTER
- UPDATE (Correct answer)
- MODIFY
Correct answer: UPDATE
UPDATE changes existing records in a table based on a specified condition in the WHERE clause.
Question 6: What does the SQL ORDER BY clause do?
- Groups rows by a column value
- Sorts result rows by one or more columns (Correct answer)
- Filters rows meeting a condition
- Removes duplicate values
Correct answer: Sorts result rows by one or more columns
ORDER BY sorts query results in ascending (ASC, default) or descending (DESC) order based on specified columns.
Which SQL clause is used to filter rows after a GROUP BY operation?