DA SQL and Database Querying 2 — Questions and Answers
Question 1: Which SQL function returns the current date and time?
- DATE()
- GETTIME()
- NOW() (Correct answer)
- CURRENT()
Correct answer: NOW()
NOW() returns the current date and time in most SQL dialects, including MySQL and PostgreSQL.
Question 2: What is a subquery in SQL?
- A query stored as a view
- A query nested inside another query (Correct answer)
- A query that joins multiple tables
- A query that modifies table structure
Correct answer: A query nested inside another query
A subquery is a query embedded within another SQL query, often in the WHERE, FROM, or SELECT clause.
Question 3: What does the GROUP BY clause do in SQL?
- Joins tables on matching columns
- Sorts the result set
- Aggregates rows sharing the same value in specified columns (Correct answer)
- Filters rows before the query executes
Correct answer: Aggregates rows sharing the same value in specified columns
GROUP BY collapses rows that have the same value in the specified column(s) so aggregate functions like SUM or COUNT apply per group.
Question 4: Which SQL join returns only the rows where there is a match in both tables?
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
- INNER JOIN (Correct answer)
Correct answer: INNER JOIN
INNER JOIN returns only rows that have matching values in both tables based on the join condition.
Question 5: What is the purpose of an index in a database?
- Enforce referential integrity
- Speed up data retrieval operations (Correct answer)
- Prevent duplicate entries
- Define the data type of a column
Correct answer: Speed up data retrieval operations
An index creates a data structure that allows the database engine to locate rows faster without scanning the entire table.
Question 6: Which SQL function calculates a running total within a window of rows?
- CUMSUM()
- SUM() OVER () (Correct answer)
- RUNNING_TOTAL()
- AGGREGATE()
Correct answer: SUM() OVER ()
SUM() OVER () is a window function that computes a cumulative sum across a defined partition or ordered window of rows.
Which SQL function returns the current date and time?