SQL Concepts Flashcards

(17 cards)

1
Q

CRUD : Create as Insert, Read as Select, Update, Delete

Difference between Delete Truncate and Drop

A

If to remove every row then use truncate because its faster, resets identity but can’t be rolled back

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

“Avoid SELECT * in production — Why

A

“Avoid SELECT * in production — it transfers extra data and can hurt performance or rely on unpredictable schema order

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

Where vs Having

A

Where filters before aggregation. It can’t filter out groups
Having filters groups after Aggregation

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

Why group by not working

A

Columns in group by must in select or Aggregate like count sum avg

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

Inner Join

A

Returns rows that have matching values in both tables. Rest are excluded

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

Left join

A

All rows from left table and matched rows from right table. If no match in right table, those columns are null

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

Right join

A

All rows from right table and matched rows from left table. If no match then left columns are null

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

Full outer join

A

All rows from both tables. If no match then missing columns are null

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

Sub query

A

Query inside another query used to fetch Intermediate results. Dangerous because they may run for every outer row and make it slow

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

CTE

A

Common table expression is named temporary result set that can be read by query for readability and recursion. They dont improve performance

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

Aggregate

A

They perform calculations on a set of rows and give single value. They can’t be combined by non Aggregate select without group by

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

Distinct vs GroupBy

A

Distinct can be slow for large tables. Group By gives better performance

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

Exists vs In

A

Exists checks if a sub query returns any row. It stops at the first match.

In compares against a list of values for the column

Exists is better for large sub queries because in is slow if list is huge or contains nulls

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

Second highest salary

A

WITH SalaryRank AS (
SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn
FROM Employees
)
SELECT salary FROM SalaryRank WHERE rn = 2;

17
Q

Delete duplicates

A

WITH CTE AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn
FROM Users
)
DELETE FROM CTE WHERE rn > 1;