SQL Flashcards

(37 cards)

1
Q

What is the difference between WHERE and HAVING?

A

WHERE filters rows before aggregation. HAVING filters after aggregation.

Example: SELECT department, COUNT() FROM employees GROUP BY department HAVING COUNT() > 10;

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

How would you find duplicate records in a table?

A

SELECT column1, COUNT() FROM table_name GROUP BY column1 HAVING COUNT() > 1.

This returns records where column1 appears more than once — useful in data quality checks.

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

What SQL functions would you use to handle missing or NULL values?

A

COALESCE() returns the first non-null value. IS NULL / IS NOT NULL to filter nulls.

Example: SELECT COALESCE(phone_number, ‘Missing’) FROM customers;

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

How would you retrieve the top 5 highest risk scores from a table?

A

SELECT * FROM risk_scores ORDER BY score DESC LIMIT 5.

In Oracle: SELECT * FROM ( SELECT * FROM risk_scores ORDER BY score DESC ) WHERE ROWNUM <= 5;

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

How would you join two tables and only include records that exist in both?

A

Use an INNER JOIN: SELECT a.id, a.name, b.balance FROM accounts a INNER JOIN balances b ON a.id = b.account_id.

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

How would you calculate the average loan amount by customer type?

A

SELECT customer_type, AVG(loan_amount) AS avg_loan FROM loans GROUP BY customer_type.

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

What’s the difference between INNER JOIN, LEFT JOIN, and FULL OUTER JOIN?

A

INNER JOIN: Only matching rows in both tables. LEFT JOIN: All rows from the left table, with NULLs for non-matches on the right. FULL OUTER JOIN: All rows from both tables; NULLs where there’s no match.

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

How would you write a query to identify accounts with transactions above the 90th percentile?

A

SELECT * FROM transactions WHERE amount > (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY amount) FROM transactions).

Not all databases support PERCENTILE_CONT; fallback could be calculating it in Python.

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

How would you check if your joins are inflating row counts?

A

Compare row counts before and after the join: SELECT COUNT() FROM table1; SELECT COUNT() FROM table2; SELECT COUNT(*) FROM table1 JOIN table2 ON condition.

Also useful: check for duplicate keys using GROUP BY key HAVING COUNT(*) > 1.

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

How would you write a query to validate that for each model run, there’s a test result logged?

A

SELECT m.model_id FROM model_runs m LEFT JOIN test_results t ON m.model_id = t.model_id WHERE t.model_id IS NULL.

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

What is a primary key

A

A unique none nullable identifier that for each record on the table consisting of one or more columns, however each table can only have one primary key

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

What is a join

A

Function that allows to combine tables based on some logic from the columns that a user defines. (Join two tables together)

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

What are the main types of join

A

Inner, outer, left, and right.

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

Difference between SQL and MYSQL

A

SQL is a coding language and MYSQL is piece of software or a relational database management tool

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

What’s the difference between DELETE and TRUNCATE?

A

DELETE is a row-by-row operation with a WHERE clause, allows rollback, is fully logged, and fires triggers. TRUNCATE is a bulk operation without a WHERE clause, sometimes allows rollback, has minimal logging, and does not fire triggers.

Use DELETE for selective removal with logic or triggers. Use TRUNCATE for fast full-table wipes.

17
Q

What is a Virtual Table?

A

A virtual table is a result set returned by a query, not a real stored table. Common examples include Views and CTEs. They represent a transformation of other data.

18
Q

What is a CTE (Common Table Expression)? Why use it?

A

A CTE is a temporary result set defined using WITH that can be referenced in a main SQL query. It improves readability, avoids repeated subqueries, and supports recursive queries. (Only accessed until the dml statement is accessed)

Example: WITH high_risk AS (SELECT * FROM risk_scores WHERE score > 90) SELECT customer_id FROM high_risk WHERE region = ‘West’;

19
Q

What are the different types of Indexes in SQL?

A

Common types of indexes include B-Tree Index, Bitmap Index, Unique Index, Composite Index, Full-Text Index, and Hash Index (in some systems). They speed up data retrieval.

20
Q

What’s the difference between a View and a CTE?

A

A View is stored in the database and can be reused across queries, while a CTE is temporary for the duration of a query and exists only within that query.

Use View for a reusable reporting layer and CTE for breaking complex logic inside one query.

21
Q

What are Primary Key and Foreign Key?

A

A Primary Key uniquely identifies each row in a table and must be unique and NOT NULL. A Foreign Key is a field in one table that refers to the Primary Key in another, enforcing referential integrity.

22
Q

What are INNER, LEFT, RIGHT, and FULL OUTER joins?

A

INNER JOIN returns only matching rows from both tables. LEFT JOIN returns all rows from the left table and matching rows from the right. RIGHT JOIN returns all rows from the right and matching from the left. FULL OUTER JOIN returns all rows from both tables, matched where possible.

23
Q

What is the logical order of SQL query execution?

A

FROM

WHERE

GROUP BY

HAVING

SELECT

DISTINCT

ORDER BY

LIMIT/OFFSET

24
Q

What’s the difference between DELETE and TRUNCATE?

A

DELETE: Row-by-row removal, supports WHERE, fully logged, slower, can be rolled back, fires triggers.

TRUNCATE: Fast, deletes all rows, minimal logging, cannot use WHERE, does not fire triggers, limited rollback.

25
What is a virtual table?
A virtual table is not stored physically. It’s a result set from a query, like a View or CTE.
26
What is a CTE (Common Table Expression)?
A temporary result set defined using WITH. Used for improving query readability, handling recursion, and avoiding repeated subqueries.
27
What are the different types of SQL ranking functions?
RANK(): Gaps in ranking after ties. DENSE_RANK(): No gaps. ROW_NUMBER(): Unique row numbers. NTILE(n): Buckets rows into n groups.
28
What is a temporary table?
A short-lived table created with CREATE TEMPORARY TABLE, used to store intermediate results. Automatically drops after session ends.
29
What is a subquery?
A query nested within another query. It can be scalar, column, row, table-level, correlated (depends on outer query), or uncorrelated.
30
What’s the difference between a correlated and uncorrelated subquery?
Correlated: Uses values from the outer query; runs once per row. Uncorrelated: Self-contained; runs once.
31
What are the SQL command subsets (categories)?
DDL: Define/modify structure (CREATE, ALTER, DROP) DML: Insert/update/delete data (INSERT, UPDATE, DELETE) DCL: Access control (GRANT, REVOKE) TCL: Transaction control (COMMIT, ROLLBACK) DQL: Query data (SELECT)
32
What is the difference between a view and a CTE?
View: Permanent virtual table stored in DB. CTE: Temporary, only exists for the duration of the query.
33
What is a database?
A structured, organized collection of data that supports efficient storage, retrieval, and manipulation, managed using a DBMS.
34
What is the difference between SQL and NoSQL?
SQL NoSQL Tables & rows Flexible models (doc, key-val) Fixed schema Dynamic schema ACID compliant BASE, eventual consistency Vertical scaling Horizontal scaling Joins supported Limited joins Examples: MySQL, Pg Examples: MongoDB, Cassandra
35
What’s the difference between RANK() and DENSE_RANK()?
RANK(): Skips rank numbers after ties. DENSE_RANK(): Does not skip rank numbers.
36
What’s the difference between ROW_NUMBER() and RANK()?
ROW_NUMBER(): Assigns unique number regardless of ties. RANK(): Assigns same number to ties and skips next ranks.
37
What’s the difference between IN and EXISTS in SQL?
IN: Compares a column to a list or result set; better for small lists. EXISTS: Returns true if any row exists; better for correlated subqueries.