SQL Basics (DQL + DML) Flashcards

SQL categories: DQL, DML, DDL, DCL, TCL SELECT, WHERE, ORDER BY, LIMIT INSERT, UPDATE, DELETE Aggregate functions: COUNT, SUM, AVG, MIN, MAX GROUP BY and HAVING JOINs: INNER, LEFT, RIGHT, FULL OUTER Aliases, DISTINCT, IS NULL vs = NULL (43 cards)

1
Q

What are the main categories of SQL?

A

DQL (Data Query Language), DML (Data Manipulation Language), DDL (Data Definition Language), DCL (Data Control Language), TCL (Transaction Control Language).

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

What is DQL in SQL?

A

DQL is used to query data from the database, primarily using the SELECT statement.

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

What is DML in SQL?

A

DML includes commands like INSERT, UPDATE, DELETE to modify the data in the database.

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

What is DDL in SQL?

A

DDL is used to define and modify database structures, such as CREATE, ALTER, DROP.

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

What is DCL in SQL?

A

DCL includes commands like GRANT and REVOKE to control access to data.

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

What is TCL in SQL?

A

TCL includes commands like COMMIT, ROLLBACK, SAVEPOINT to manage transactions.

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

What does the SELECT statement do?

A

SELECT retrieves data from one or more tables.

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

What does the WHERE clause do in SQL?

A

WHERE filters rows based on specified conditions.

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

What does ORDER BY do?

A

ORDER BY sorts the result set based on one or more columns.

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

What does the LIMIT clause do?

A

LIMIT restricts the number of rows returned in a result set.

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

What does INSERT do in SQL?

A

INSERT adds new rows to a table.

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

What does UPDATE do in SQL?

A

UPDATE modifies existing rows in a table.

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

What does DELETE do in SQL?

A

DELETE removes rows from a table.

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

What does COUNT() do in SQL?

A

COUNT() returns the number of rows matching a condition.

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

What does SUM() do in SQL?

A

SUM() returns the total sum of a numeric column.

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

What does AVG() do in SQL?

A

AVG() returns the average value of a numeric column.

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

What does MIN() do in SQL?

A

MIN() returns the smallest value in a column.

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

What does MAX() do in SQL?

A

MAX() returns the largest value in a column.

19
Q

What does GROUP BY do?

A

GROUP BY groups rows that have the same values into summary rows.

20
Q

What does HAVING do?

A

HAVING filters groups based on aggregate conditions.

21
Q

What is an INNER JOIN?

A

Returns rows with matching values in both tables.

22
Q

What is a LEFT JOIN?

A

Returns all rows from the left table and matched rows from the right table.

23
Q

What is a RIGHT JOIN?

A

Returns all rows from the right table and matched rows from the left table.

24
Q

What is a FULL OUTER JOIN?

A

Returns all rows when there is a match in either left or right table.

25
What are SQL aliases?
Temporary names for columns or tables using AS keyword for readability.
26
What does DISTINCT do in SQL?
DISTINCT returns only unique values, removing duplicates from the result set.
27
What’s the difference between IS NULL and = NULL?
IS NULL checks for NULL values; = NULL does not work because NULL is not equal to anything.
28
What is a best practice when using JOINs?
Always specify join conditions to avoid Cartesian products.
29
What is a best practice for writing queries?
Use explicit column names instead of SELECT * for better performance and clarity.
30
What are common use cases for aggregate functions?
Analytics, reporting, and summary tables.
31
What is the impact of poorly written queries on system design?
Can lead to high CPU usage, slow response times, and inefficient indexes.
32
How do JOINs affect performance?
JOINs can be expensive on large tables if not indexed properly or filtered early.
33
What tools help monitor SQL query performance?
EXPLAIN plans, SQL profiler tools, slow query logs, and APM tools.
34
What’s a tradeoff of using GROUP BY with large datasets?
High memory/CPU usage versus pre-aggregating data in summary tables.
35
What is a common interview question on JOINs?
Explain the difference between INNER JOIN and LEFT JOIN with an example.
36
What is a potential gotcha with UPDATE statements?
Forgetting the WHERE clause can update all rows in a table.
37
What is a potential gotcha with DELETE statements?
Running DELETE without WHERE deletes all data from the table.
38
What’s a performance tip for aggregate queries?
Use indexes on grouped and filtered columns where possible.
39
What is a common real-world tradeoff in query design?
Read performance vs. flexibility and maintainability of the SQL code.
40
When should you use DISTINCT cautiously?
DISTINCT may be expensive and should be avoided unless necessary to remove duplicates.
41
What does IS NOT NULL do?
Checks whether a value is not NULL.
42
What is a scalar subquery in SELECT?
A subquery that returns a single value, used in SELECT or WHERE clauses.
43
What’s a use case for ORDER BY with LIMIT?
Fetching top N results like 'Top 10 best-selling products'.