Interview Prep

SQL Interview Questions for Software Engineer (2026)

15 Questions
With Answers
5 Basic5 Intermediate5 Advanced

Basic SQL Questions

Q1. What is normalization and why is it important?

basic

Answer

Normalization organizes data to reduce redundancy and improve integrity. Normal forms: 1NF (atomic values), 2NF (no partial dependencies), 3NF (no transitive dependencies). Benefits: Less storage, easier updates, data consistency. Trade-off: May need JOINs for queries.

Example Code

SQL
1-- Unnormalized (bad)
2CREATE TABLE orders (
3  id INT, customer_name VARCHAR, customer_email VARCHAR,
4  product_name VARCHAR, product_price DECIMAL
5);
6
7-- Normalized (good) - 3NF
8CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
9CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR, price DECIMAL);
10CREATE TABLE orders (
11  id INT PRIMARY KEY,
12  customer_id INT REFERENCES customers(id),
13  product_id INT REFERENCES products(id)
14);

Q1. What is normalization and why is it important?

basic

Answer

Normalization organizes data to reduce redundancy and improve integrity. Normal forms: 1NF (atomic values), 2NF (no partial dependencies), 3NF (no transitive dependencies). Benefits: Less storage, easier updates, data consistency. Trade-off: May need JOINs for queries.

Example Code

SQL
1-- Unnormalized (bad)
2CREATE TABLE orders (
3  id INT, customer_name VARCHAR, customer_email VARCHAR,
4  product_name VARCHAR, product_price DECIMAL
5);
6
7-- Normalized (good) - 3NF
8CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
9CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR, price DECIMAL);
10CREATE TABLE orders (
11  id INT PRIMARY KEY,
12  customer_id INT REFERENCES customers(id),
13  product_id INT REFERENCES products(id)
14);

Q1. What is normalization and why is it important?

basic

Answer

Normalization organizes data to reduce redundancy and improve integrity. Normal forms: 1NF (atomic values), 2NF (no partial dependencies), 3NF (no transitive dependencies). Benefits: Less storage, easier updates, data consistency. Trade-off: May need JOINs for queries.

Example Code

SQL
1-- Unnormalized (bad)
2CREATE TABLE orders (
3  id INT, customer_name VARCHAR, customer_email VARCHAR,
4  product_name VARCHAR, product_price DECIMAL
5);
6
7-- Normalized (good) - 3NF
8CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
9CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR, price DECIMAL);
10CREATE TABLE orders (
11  id INT PRIMARY KEY,
12  customer_id INT REFERENCES customers(id),
13  product_id INT REFERENCES products(id)
14);

Q1. What is normalization and why is it important?

basic

Answer

Normalization organizes data to reduce redundancy and improve integrity. Normal forms: 1NF (atomic values), 2NF (no partial dependencies), 3NF (no transitive dependencies). Benefits: Less storage, easier updates, data consistency. Trade-off: May need JOINs for queries.

Example Code

SQL
1-- Unnormalized (bad)
2CREATE TABLE orders (
3  id INT, customer_name VARCHAR, customer_email VARCHAR,
4  product_name VARCHAR, product_price DECIMAL
5);
6
7-- Normalized (good) - 3NF
8CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
9CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR, price DECIMAL);
10CREATE TABLE orders (
11  id INT PRIMARY KEY,
12  customer_id INT REFERENCES customers(id),
13  product_id INT REFERENCES products(id)
14);

Q1. What is normalization and why is it important?

basic

Answer

Normalization organizes data to reduce redundancy and improve integrity. Normal forms: 1NF (atomic values), 2NF (no partial dependencies), 3NF (no transitive dependencies). Benefits: Less storage, easier updates, data consistency. Trade-off: May need JOINs for queries.

Example Code

SQL
1-- Unnormalized (bad)
2CREATE TABLE orders (
3  id INT, customer_name VARCHAR, customer_email VARCHAR,
4  product_name VARCHAR, product_price DECIMAL
5);
6
7-- Normalized (good) - 3NF
8CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
9CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR, price DECIMAL);
10CREATE TABLE orders (
11  id INT PRIMARY KEY,
12  customer_id INT REFERENCES customers(id),
13  product_id INT REFERENCES products(id)
14);

Intermediate SQL Questions

Q2. Write a query to find the Nth highest salary in a company.

intermediate

Answer

Multiple approaches: 1) Subquery with COUNT, 2) OFFSET/LIMIT, 3) Window function with DENSE_RANK. Window function is most flexible for handling ties.

Example Code

SQL
1-- Method 1: Using OFFSET (N=3 for 3rd highest)
2SELECT DISTINCT salary FROM employees
3ORDER BY salary DESC LIMIT 1 OFFSET 2;
4
5-- Method 2: Using subquery
6SELECT MAX(salary) FROM employees
7WHERE salary < (SELECT MAX(salary) FROM employees); -- 2nd highest
8
9-- Method 3: Using window function (handles ties)
10WITH ranked AS (
11  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
12  FROM employees
13)
14SELECT DISTINCT salary FROM ranked WHERE rank = 3;

Q2. Write a query to find the Nth highest salary in a company.

intermediate

Answer

Multiple approaches: 1) Subquery with COUNT, 2) OFFSET/LIMIT, 3) Window function with DENSE_RANK. Window function is most flexible for handling ties.

Example Code

SQL
1-- Method 1: Using OFFSET (N=3 for 3rd highest)
2SELECT DISTINCT salary FROM employees
3ORDER BY salary DESC LIMIT 1 OFFSET 2;
4
5-- Method 2: Using subquery
6SELECT MAX(salary) FROM employees
7WHERE salary < (SELECT MAX(salary) FROM employees); -- 2nd highest
8
9-- Method 3: Using window function (handles ties)
10WITH ranked AS (
11  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
12  FROM employees
13)
14SELECT DISTINCT salary FROM ranked WHERE rank = 3;

Q2. Write a query to find the Nth highest salary in a company.

intermediate

Answer

Multiple approaches: 1) Subquery with COUNT, 2) OFFSET/LIMIT, 3) Window function with DENSE_RANK. Window function is most flexible for handling ties.

Example Code

SQL
1-- Method 1: Using OFFSET (N=3 for 3rd highest)
2SELECT DISTINCT salary FROM employees
3ORDER BY salary DESC LIMIT 1 OFFSET 2;
4
5-- Method 2: Using subquery
6SELECT MAX(salary) FROM employees
7WHERE salary < (SELECT MAX(salary) FROM employees); -- 2nd highest
8
9-- Method 3: Using window function (handles ties)
10WITH ranked AS (
11  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
12  FROM employees
13)
14SELECT DISTINCT salary FROM ranked WHERE rank = 3;

Q2. Write a query to find the Nth highest salary in a company.

intermediate

Answer

Multiple approaches: 1) Subquery with COUNT, 2) OFFSET/LIMIT, 3) Window function with DENSE_RANK. Window function is most flexible for handling ties.

Example Code

SQL
1-- Method 1: Using OFFSET (N=3 for 3rd highest)
2SELECT DISTINCT salary FROM employees
3ORDER BY salary DESC LIMIT 1 OFFSET 2;
4
5-- Method 2: Using subquery
6SELECT MAX(salary) FROM employees
7WHERE salary < (SELECT MAX(salary) FROM employees); -- 2nd highest
8
9-- Method 3: Using window function (handles ties)
10WITH ranked AS (
11  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
12  FROM employees
13)
14SELECT DISTINCT salary FROM ranked WHERE rank = 3;

Q2. Write a query to find the Nth highest salary in a company.

intermediate

Answer

Multiple approaches: 1) Subquery with COUNT, 2) OFFSET/LIMIT, 3) Window function with DENSE_RANK. Window function is most flexible for handling ties.

Example Code

SQL
1-- Method 1: Using OFFSET (N=3 for 3rd highest)
2SELECT DISTINCT salary FROM employees
3ORDER BY salary DESC LIMIT 1 OFFSET 2;
4
5-- Method 2: Using subquery
6SELECT MAX(salary) FROM employees
7WHERE salary < (SELECT MAX(salary) FROM employees); -- 2nd highest
8
9-- Method 3: Using window function (handles ties)
10WITH ranked AS (
11  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
12  FROM employees
13)
14SELECT DISTINCT salary FROM ranked WHERE rank = 3;

Advanced SQL Questions

Q3. How would you design a database schema for a social media feed?

advanced

Answer

Consider: Users table, Posts table with user_id FK, Follows table (follower_id, following_id), Likes/Comments as junction tables. For feed: denormalize into timeline table or use fan-out-on-write pattern. Index on followed_id and created_at.

Example Code

SQL
1CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR UNIQUE);
2
3CREATE TABLE follows (
4  follower_id INT REFERENCES users(id),
5  following_id INT REFERENCES users(id),
6  PRIMARY KEY (follower_id, following_id)
7);
8
9CREATE TABLE posts (
10  id SERIAL PRIMARY KEY,
11  user_id INT REFERENCES users(id),
12  content TEXT,
13  created_at TIMESTAMP DEFAULT NOW()
14);
15
16-- Get feed for user 1
17SELECT p.* FROM posts p
18JOIN follows f ON p.user_id = f.following_id
19WHERE f.follower_id = 1
20ORDER BY p.created_at DESC LIMIT 50;
21
22CREATE INDEX idx_posts_user_date ON posts(user_id, created_at DESC);

Q3. How would you design a database schema for a social media feed?

advanced

Answer

Consider: Users table, Posts table with user_id FK, Follows table (follower_id, following_id), Likes/Comments as junction tables. For feed: denormalize into timeline table or use fan-out-on-write pattern. Index on followed_id and created_at.

Example Code

SQL
1CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR UNIQUE);
2
3CREATE TABLE follows (
4  follower_id INT REFERENCES users(id),
5  following_id INT REFERENCES users(id),
6  PRIMARY KEY (follower_id, following_id)
7);
8
9CREATE TABLE posts (
10  id SERIAL PRIMARY KEY,
11  user_id INT REFERENCES users(id),
12  content TEXT,
13  created_at TIMESTAMP DEFAULT NOW()
14);
15
16-- Get feed for user 1
17SELECT p.* FROM posts p
18JOIN follows f ON p.user_id = f.following_id
19WHERE f.follower_id = 1
20ORDER BY p.created_at DESC LIMIT 50;
21
22CREATE INDEX idx_posts_user_date ON posts(user_id, created_at DESC);

Q3. How would you design a database schema for a social media feed?

advanced

Answer

Consider: Users table, Posts table with user_id FK, Follows table (follower_id, following_id), Likes/Comments as junction tables. For feed: denormalize into timeline table or use fan-out-on-write pattern. Index on followed_id and created_at.

Example Code

SQL
1CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR UNIQUE);
2
3CREATE TABLE follows (
4  follower_id INT REFERENCES users(id),
5  following_id INT REFERENCES users(id),
6  PRIMARY KEY (follower_id, following_id)
7);
8
9CREATE TABLE posts (
10  id SERIAL PRIMARY KEY,
11  user_id INT REFERENCES users(id),
12  content TEXT,
13  created_at TIMESTAMP DEFAULT NOW()
14);
15
16-- Get feed for user 1
17SELECT p.* FROM posts p
18JOIN follows f ON p.user_id = f.following_id
19WHERE f.follower_id = 1
20ORDER BY p.created_at DESC LIMIT 50;
21
22CREATE INDEX idx_posts_user_date ON posts(user_id, created_at DESC);

Q3. How would you design a database schema for a social media feed?

advanced

Answer

Consider: Users table, Posts table with user_id FK, Follows table (follower_id, following_id), Likes/Comments as junction tables. For feed: denormalize into timeline table or use fan-out-on-write pattern. Index on followed_id and created_at.

Example Code

SQL
1CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR UNIQUE);
2
3CREATE TABLE follows (
4  follower_id INT REFERENCES users(id),
5  following_id INT REFERENCES users(id),
6  PRIMARY KEY (follower_id, following_id)
7);
8
9CREATE TABLE posts (
10  id SERIAL PRIMARY KEY,
11  user_id INT REFERENCES users(id),
12  content TEXT,
13  created_at TIMESTAMP DEFAULT NOW()
14);
15
16-- Get feed for user 1
17SELECT p.* FROM posts p
18JOIN follows f ON p.user_id = f.following_id
19WHERE f.follower_id = 1
20ORDER BY p.created_at DESC LIMIT 50;
21
22CREATE INDEX idx_posts_user_date ON posts(user_id, created_at DESC);

Q3. How would you design a database schema for a social media feed?

advanced

Answer

Consider: Users table, Posts table with user_id FK, Follows table (follower_id, following_id), Likes/Comments as junction tables. For feed: denormalize into timeline table or use fan-out-on-write pattern. Index on followed_id and created_at.

Example Code

SQL
1CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR UNIQUE);
2
3CREATE TABLE follows (
4  follower_id INT REFERENCES users(id),
5  following_id INT REFERENCES users(id),
6  PRIMARY KEY (follower_id, following_id)
7);
8
9CREATE TABLE posts (
10  id SERIAL PRIMARY KEY,
11  user_id INT REFERENCES users(id),
12  content TEXT,
13  created_at TIMESTAMP DEFAULT NOW()
14);
15
16-- Get feed for user 1
17SELECT p.* FROM posts p
18JOIN follows f ON p.user_id = f.following_id
19WHERE f.follower_id = 1
20ORDER BY p.created_at DESC LIMIT 50;
21
22CREATE INDEX idx_posts_user_date ON posts(user_id, created_at DESC);

Interview Tips

  • Practice writing queries without an IDE to simulate whiteboard interviews
  • Explain your thought process as you solve problems
  • Ask clarifying questions about edge cases
  • Consider query performance and scalability

Related Content

From Our Blog

Ready for your interview?

Practice with our interactive SQL sandbox and get instant feedback.