SQL Query Flashcards

(34 cards)

2
Q

What is querying in SQL?

A

The act of retrieving specific data from a database using SELECT-based commands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the full structure of an SQL query?

A

SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which clause is executed last in SQL?

A

SELECT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is SELECT executed last?

A

Because it operates on the table that has already been filtered, grouped, and sorted by previous clauses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the SELECT clause do?

A

It retrieves specific columns, expressions, or function outputs from the processed table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does DISTINCT do in SELECT?

A

Removes duplicate results from the returned set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does AS do in SELECT?

A

Assigns a temporary alias to a column or expression for readability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the FROM clause?

A

It tells SQL which table(s) to retrieve data from.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is the FROM clause necessary even when the table name is mentioned?

A

Because SQL needs an explicit instruction to locate data sources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the WHERE clause do?

A

Filters rows before grouping, keeping only rows where conditions are true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give examples of condition types usable in WHERE.

A

Comparison operators (> < =), logic (AND, OR), IN, BETWEEN, IS NULL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the GROUP BY clause do?

A

It groups rows based on matching values in one or more columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What happens if you GROUP BY multiple columns?

A

SQL groups rows that match on all specified columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the HAVING clause do?

A

Filters groups after GROUP BY has created them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you use HAVING without GROUP BY?

A

Yes, it then treats the result set as one big group.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Why can’t WHERE be used after GROUP BY?

A

Because WHERE works on individual rows, not groups.

18
Q

What does ORDER BY do?

A

Sorts the result rows based on column values.

19
Q

What is the default sort order in ORDER BY?

A

Ascending (ASC).

20
Q

How do you sort descending?

A

Use DESC after the column name.

21
Q

What happens when sorting by multiple columns?

A

SQL sorts by the first column, then breaks ties using the next.

22
Q

What does the LIMIT clause do?

A

Restricts the number of rows returned.

23
Q

How can you skip a number of rows before returning results?

A

Use OFFSET with LIMIT: LIMIT offset, count

24
Q

What are aggregate functions used for?

A

To summarize or perform operations on groups of rows.

25
Q

Give 5 aggregate functions.

A

COUNT(), SUM(), AVG(), MIN(), MAX()

26
If you want to find employees in 'Sales', what clause do you use?
WHERE department = 'Sales'
27
If you want to see departments with total salaries over $100k?
Use GROUP BY department and HAVING SUM(salary) > 100000
28
If you want the top 3 oldest employees?
ORDER BY age DESC LIMIT 3
29
Spot the mistake: 'SELECT * employees;'
Missing FROM clause: should be SELECT * FROM employees;
30
Spot the mistake: 'ORDER name BY ASC;'
Correct form is: ORDER BY name ASC;
31
List the execution order of SQL SELECT query parts.
FROM → WHERE → GROUP BY → HAVING → ORDER BY → SELECT → LIMIT
32
Explain the difference between WHERE and HAVING.
WHERE filters rows before grouping; HAVING filters after groups are formed.
33
Describe the role of GROUP BY in data summarization.
It segments data by categories so aggregate functions can operate on each group.
34
If you forget GROUP BY when using HAVING, what happens?
SQL treats the entire table as one group.
35
If you omit WHERE and HAVING, what filtering is applied?
None. All rows (and groups) are included.