Interview Prep

SQL Interview Questions for Freshers / Entry Level (2026)

25 Questions
With Answers
15 Basic10 Intermediate

Basic SQL Questions

Q1. What is SQL and what are its main components?

basic

Answer

SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).

Example Code

SQL
1-- DDL: Create structure
2CREATE TABLE students (id INT, name VARCHAR(100));
3
4-- DML: Manipulate data
5INSERT INTO students VALUES (1, 'John');
6SELECT * FROM students;
7
8-- DCL: Control access
9GRANT SELECT ON students TO app_user;
10
11-- TCL: Transaction control
12BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;

Q1. What is SQL and what are its main components?

basic

Answer

SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).

Example Code

SQL
1-- DDL: Create structure
2CREATE TABLE students (id INT, name VARCHAR(100));
3
4-- DML: Manipulate data
5INSERT INTO students VALUES (1, 'John');
6SELECT * FROM students;
7
8-- DCL: Control access
9GRANT SELECT ON students TO app_user;
10
11-- TCL: Transaction control
12BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;

Q1. What is SQL and what are its main components?

basic

Answer

SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).

Example Code

SQL
1-- DDL: Create structure
2CREATE TABLE students (id INT, name VARCHAR(100));
3
4-- DML: Manipulate data
5INSERT INTO students VALUES (1, 'John');
6SELECT * FROM students;
7
8-- DCL: Control access
9GRANT SELECT ON students TO app_user;
10
11-- TCL: Transaction control
12BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;

Q1. What is SQL and what are its main components?

basic

Answer

SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).

Example Code

SQL
1-- DDL: Create structure
2CREATE TABLE students (id INT, name VARCHAR(100));
3
4-- DML: Manipulate data
5INSERT INTO students VALUES (1, 'John');
6SELECT * FROM students;
7
8-- DCL: Control access
9GRANT SELECT ON students TO app_user;
10
11-- TCL: Transaction control
12BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;

Q1. What is SQL and what are its main components?

basic

Answer

SQL (Structured Query Language) is used to communicate with databases. Main components: DDL (CREATE, ALTER, DROP - structure), DML (SELECT, INSERT, UPDATE, DELETE - data), DCL (GRANT, REVOKE - permissions), TCL (COMMIT, ROLLBACK - transactions).

Example Code

SQL
1-- DDL: Create structure
2CREATE TABLE students (id INT, name VARCHAR(100));
3
4-- DML: Manipulate data
5INSERT INTO students VALUES (1, 'John');
6SELECT * FROM students;
7
8-- DCL: Control access
9GRANT SELECT ON students TO app_user;
10
11-- TCL: Transaction control
12BEGIN; UPDATE students SET name = 'Jane' WHERE id = 1; COMMIT;

Q2. What is the difference between CHAR and VARCHAR?

basic

Answer

CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).

Example Code

SQL
1CREATE TABLE example (
2  country_code CHAR(2),     -- Always 2 chars: 'US', 'UK'
3  name VARCHAR(100)         -- Variable: 'John' uses 4 chars
4);
5
6-- CHAR pads with spaces
7SELECT LENGTH(country_code) FROM example; -- Returns 2
8SELECT country_code = 'US' FROM example;  -- Works (space-padded comparison)

Q2. What is the difference between CHAR and VARCHAR?

basic

Answer

CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).

Example Code

SQL
1CREATE TABLE example (
2  country_code CHAR(2),     -- Always 2 chars: 'US', 'UK'
3  name VARCHAR(100)         -- Variable: 'John' uses 4 chars
4);
5
6-- CHAR pads with spaces
7SELECT LENGTH(country_code) FROM example; -- Returns 2
8SELECT country_code = 'US' FROM example;  -- Works (space-padded comparison)

Q2. What is the difference between CHAR and VARCHAR?

basic

Answer

CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).

Example Code

SQL
1CREATE TABLE example (
2  country_code CHAR(2),     -- Always 2 chars: 'US', 'UK'
3  name VARCHAR(100)         -- Variable: 'John' uses 4 chars
4);
5
6-- CHAR pads with spaces
7SELECT LENGTH(country_code) FROM example; -- Returns 2
8SELECT country_code = 'US' FROM example;  -- Works (space-padded comparison)

Q2. What is the difference between CHAR and VARCHAR?

basic

Answer

CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).

Example Code

SQL
1CREATE TABLE example (
2  country_code CHAR(2),     -- Always 2 chars: 'US', 'UK'
3  name VARCHAR(100)         -- Variable: 'John' uses 4 chars
4);
5
6-- CHAR pads with spaces
7SELECT LENGTH(country_code) FROM example; -- Returns 2
8SELECT country_code = 'US' FROM example;  -- Works (space-padded comparison)

Q2. What is the difference between CHAR and VARCHAR?

basic

Answer

CHAR is fixed-length (pads with spaces), VARCHAR is variable-length (stores actual length). CHAR(10) always uses 10 bytes, VARCHAR(10) uses actual string length + overhead. Use CHAR for fixed-length data (country codes), VARCHAR for variable (names).

Example Code

SQL
1CREATE TABLE example (
2  country_code CHAR(2),     -- Always 2 chars: 'US', 'UK'
3  name VARCHAR(100)         -- Variable: 'John' uses 4 chars
4);
5
6-- CHAR pads with spaces
7SELECT LENGTH(country_code) FROM example; -- Returns 2
8SELECT country_code = 'US' FROM example;  -- Works (space-padded comparison)

Q3. What is NULL in SQL and how do you check for it?

basic

Answer

NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.

Example Code

SQL
1-- Checking for NULL
2SELECT * FROM users WHERE phone IS NULL;
3SELECT * FROM users WHERE phone IS NOT NULL;
4
5-- WRONG: This never matches
6SELECT * FROM users WHERE phone = NULL;
7
8-- Handling NULL in calculations
9SELECT COALESCE(phone, 'N/A') FROM users;
10SELECT salary + COALESCE(bonus, 0) AS total FROM employees;

Q3. What is NULL in SQL and how do you check for it?

basic

Answer

NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.

Example Code

SQL
1-- Checking for NULL
2SELECT * FROM users WHERE phone IS NULL;
3SELECT * FROM users WHERE phone IS NOT NULL;
4
5-- WRONG: This never matches
6SELECT * FROM users WHERE phone = NULL;
7
8-- Handling NULL in calculations
9SELECT COALESCE(phone, 'N/A') FROM users;
10SELECT salary + COALESCE(bonus, 0) AS total FROM employees;

Q3. What is NULL in SQL and how do you check for it?

basic

Answer

NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.

Example Code

SQL
1-- Checking for NULL
2SELECT * FROM users WHERE phone IS NULL;
3SELECT * FROM users WHERE phone IS NOT NULL;
4
5-- WRONG: This never matches
6SELECT * FROM users WHERE phone = NULL;
7
8-- Handling NULL in calculations
9SELECT COALESCE(phone, 'N/A') FROM users;
10SELECT salary + COALESCE(bonus, 0) AS total FROM employees;

Q3. What is NULL in SQL and how do you check for it?

basic

Answer

NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.

Example Code

SQL
1-- Checking for NULL
2SELECT * FROM users WHERE phone IS NULL;
3SELECT * FROM users WHERE phone IS NOT NULL;
4
5-- WRONG: This never matches
6SELECT * FROM users WHERE phone = NULL;
7
8-- Handling NULL in calculations
9SELECT COALESCE(phone, 'N/A') FROM users;
10SELECT salary + COALESCE(bonus, 0) AS total FROM employees;

Q3. What is NULL in SQL and how do you check for it?

basic

Answer

NULL represents unknown or missing value. It's not equal to zero or empty string. Use IS NULL or IS NOT NULL to check (= NULL doesn't work). NULL in any calculation returns NULL. Use COALESCE() or IFNULL() to provide defaults.

Example Code

SQL
1-- Checking for NULL
2SELECT * FROM users WHERE phone IS NULL;
3SELECT * FROM users WHERE phone IS NOT NULL;
4
5-- WRONG: This never matches
6SELECT * FROM users WHERE phone = NULL;
7
8-- Handling NULL in calculations
9SELECT COALESCE(phone, 'N/A') FROM users;
10SELECT salary + COALESCE(bonus, 0) AS total FROM employees;

Intermediate SQL Questions

Q4. What is a foreign key and why is it used?

intermediate

Answer

A foreign key is a column that references the primary key of another table. It enforces referential integrity (can't insert invalid references, can't delete referenced rows). Creates relationships between tables (one-to-many, many-to-many).

Example Code

SQL
1CREATE TABLE departments (
2  id INT PRIMARY KEY,
3  name VARCHAR(50)
4);
5
6CREATE TABLE employees (
7  id INT PRIMARY KEY,
8  name VARCHAR(100),
9  department_id INT,
10  FOREIGN KEY (department_id) REFERENCES departments(id)
11    ON DELETE SET NULL
12    ON UPDATE CASCADE
13);
14
15-- This fails if department 999 doesn't exist:
16INSERT INTO employees (id, name, department_id) VALUES (1, 'John', 999);

Q4. What is a foreign key and why is it used?

intermediate

Answer

A foreign key is a column that references the primary key of another table. It enforces referential integrity (can't insert invalid references, can't delete referenced rows). Creates relationships between tables (one-to-many, many-to-many).

Example Code

SQL
1CREATE TABLE departments (
2  id INT PRIMARY KEY,
3  name VARCHAR(50)
4);
5
6CREATE TABLE employees (
7  id INT PRIMARY KEY,
8  name VARCHAR(100),
9  department_id INT,
10  FOREIGN KEY (department_id) REFERENCES departments(id)
11    ON DELETE SET NULL
12    ON UPDATE CASCADE
13);
14
15-- This fails if department 999 doesn't exist:
16INSERT INTO employees (id, name, department_id) VALUES (1, 'John', 999);

Q4. What is a foreign key and why is it used?

intermediate

Answer

A foreign key is a column that references the primary key of another table. It enforces referential integrity (can't insert invalid references, can't delete referenced rows). Creates relationships between tables (one-to-many, many-to-many).

Example Code

SQL
1CREATE TABLE departments (
2  id INT PRIMARY KEY,
3  name VARCHAR(50)
4);
5
6CREATE TABLE employees (
7  id INT PRIMARY KEY,
8  name VARCHAR(100),
9  department_id INT,
10  FOREIGN KEY (department_id) REFERENCES departments(id)
11    ON DELETE SET NULL
12    ON UPDATE CASCADE
13);
14
15-- This fails if department 999 doesn't exist:
16INSERT INTO employees (id, name, department_id) VALUES (1, 'John', 999);

Q4. What is a foreign key and why is it used?

intermediate

Answer

A foreign key is a column that references the primary key of another table. It enforces referential integrity (can't insert invalid references, can't delete referenced rows). Creates relationships between tables (one-to-many, many-to-many).

Example Code

SQL
1CREATE TABLE departments (
2  id INT PRIMARY KEY,
3  name VARCHAR(50)
4);
5
6CREATE TABLE employees (
7  id INT PRIMARY KEY,
8  name VARCHAR(100),
9  department_id INT,
10  FOREIGN KEY (department_id) REFERENCES departments(id)
11    ON DELETE SET NULL
12    ON UPDATE CASCADE
13);
14
15-- This fails if department 999 doesn't exist:
16INSERT INTO employees (id, name, department_id) VALUES (1, 'John', 999);

Q4. What is a foreign key and why is it used?

intermediate

Answer

A foreign key is a column that references the primary key of another table. It enforces referential integrity (can't insert invalid references, can't delete referenced rows). Creates relationships between tables (one-to-many, many-to-many).

Example Code

SQL
1CREATE TABLE departments (
2  id INT PRIMARY KEY,
3  name VARCHAR(50)
4);
5
6CREATE TABLE employees (
7  id INT PRIMARY KEY,
8  name VARCHAR(100),
9  department_id INT,
10  FOREIGN KEY (department_id) REFERENCES departments(id)
11    ON DELETE SET NULL
12    ON UPDATE CASCADE
13);
14
15-- This fails if department 999 doesn't exist:
16INSERT INTO employees (id, name, department_id) VALUES (1, 'John', 999);

Q5. Explain UNION vs UNION ALL.

intermediate

Answer

Both combine result sets from multiple queries. UNION removes duplicates (slower, requires sorting). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL when you know there are no duplicates or need all rows.

Example Code

SQL
1-- UNION: Removes duplicates
2SELECT city FROM customers
3UNION
4SELECT city FROM suppliers;
5
6-- UNION ALL: Keeps duplicates (faster)
7SELECT email FROM active_users
8UNION ALL
9SELECT email FROM newsletter_subscribers;
10
11-- Requirements: Same number of columns, compatible types
12-- Column names come from first query

Q5. Explain UNION vs UNION ALL.

intermediate

Answer

Both combine result sets from multiple queries. UNION removes duplicates (slower, requires sorting). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL when you know there are no duplicates or need all rows.

Example Code

SQL
1-- UNION: Removes duplicates
2SELECT city FROM customers
3UNION
4SELECT city FROM suppliers;
5
6-- UNION ALL: Keeps duplicates (faster)
7SELECT email FROM active_users
8UNION ALL
9SELECT email FROM newsletter_subscribers;
10
11-- Requirements: Same number of columns, compatible types
12-- Column names come from first query

Q5. Explain UNION vs UNION ALL.

intermediate

Answer

Both combine result sets from multiple queries. UNION removes duplicates (slower, requires sorting). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL when you know there are no duplicates or need all rows.

Example Code

SQL
1-- UNION: Removes duplicates
2SELECT city FROM customers
3UNION
4SELECT city FROM suppliers;
5
6-- UNION ALL: Keeps duplicates (faster)
7SELECT email FROM active_users
8UNION ALL
9SELECT email FROM newsletter_subscribers;
10
11-- Requirements: Same number of columns, compatible types
12-- Column names come from first query

Q5. Explain UNION vs UNION ALL.

intermediate

Answer

Both combine result sets from multiple queries. UNION removes duplicates (slower, requires sorting). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL when you know there are no duplicates or need all rows.

Example Code

SQL
1-- UNION: Removes duplicates
2SELECT city FROM customers
3UNION
4SELECT city FROM suppliers;
5
6-- UNION ALL: Keeps duplicates (faster)
7SELECT email FROM active_users
8UNION ALL
9SELECT email FROM newsletter_subscribers;
10
11-- Requirements: Same number of columns, compatible types
12-- Column names come from first query

Q5. Explain UNION vs UNION ALL.

intermediate

Answer

Both combine result sets from multiple queries. UNION removes duplicates (slower, requires sorting). UNION ALL keeps all rows including duplicates (faster). Use UNION ALL when you know there are no duplicates or need all rows.

Example Code

SQL
1-- UNION: Removes duplicates
2SELECT city FROM customers
3UNION
4SELECT city FROM suppliers;
5
6-- UNION ALL: Keeps duplicates (faster)
7SELECT email FROM active_users
8UNION ALL
9SELECT email FROM newsletter_subscribers;
10
11-- Requirements: Same number of columns, compatible types
12-- Column names come from first query

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.