SQL Query Flashcards
(34 cards)
What is querying in SQL?
The act of retrieving specific data from a database using SELECT-based commands.
What is the full structure of an SQL query?
SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT …
Which clause is executed last in SQL?
SELECT.
Why is SELECT executed last?
Because it operates on the table that has already been filtered, grouped, and sorted by previous clauses.
What does the SELECT clause do?
It retrieves specific columns, expressions, or function outputs from the processed table.
What does DISTINCT do in SELECT?
Removes duplicate results from the returned set.
What does AS do in SELECT?
Assigns a temporary alias to a column or expression for readability.
What is the purpose of the FROM clause?
It tells SQL which table(s) to retrieve data from.
Why is the FROM clause necessary even when the table name is mentioned?
Because SQL needs an explicit instruction to locate data sources.
What does the WHERE clause do?
Filters rows before grouping, keeping only rows where conditions are true.
Give examples of condition types usable in WHERE.
Comparison operators (> < =), logic (AND, OR), IN, BETWEEN, IS NULL.
What does the GROUP BY clause do?
It groups rows based on matching values in one or more columns.
What happens if you GROUP BY multiple columns?
SQL groups rows that match on all specified columns.
What does the HAVING clause do?
Filters groups after GROUP BY has created them.
Can you use HAVING without GROUP BY?
Yes, it then treats the result set as one big group.
Why can’t WHERE be used after GROUP BY?
Because WHERE works on individual rows, not groups.
What does ORDER BY do?
Sorts the result rows based on column values.
What is the default sort order in ORDER BY?
Ascending (ASC).
How do you sort descending?
Use DESC after the column name.
What happens when sorting by multiple columns?
SQL sorts by the first column, then breaks ties using the next.
What does the LIMIT clause do?
Restricts the number of rows returned.
How can you skip a number of rows before returning results?
Use OFFSET with LIMIT: LIMIT offset, count
What are aggregate functions used for?
To summarize or perform operations on groups of rows.
Give 5 aggregate functions.
COUNT(), SUM(), AVG(), MIN(), MAX()