0% found this document useful (0 votes)
4 views6 pages

SQL Interview Guide

Uploaded by

bvishwkarma202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

SQL Interview Guide

Uploaded by

bvishwkarma202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL Interview Preparation Guide (Beginner to Intermediate)

1. Introduction to SQL

Theory: SQL (Structured Query Language) is used to interact with databases. It allows you to create,
read, update, and delete data from relational databases.

Example:

SELECT * FROM employees;

Interview Q&A:

• Q: What is SQL?
• A: SQL is a standard language used to interact with relational databases.

2. Data Types

Theory: SQL supports various data types such as:

• INT – Integer
• VARCHAR(n) – Variable-length string
• DATE – Date value
• FLOAT – Floating point number

Example:

CREATE TABLE students (


id INT,
name VARCHAR(100),
dob DATE
);

Interview Q&A:

• Q: What is the difference between CHAR and VARCHAR ?


• A: CHAR is fixed-length, VARCHAR is variable-length.

3. DDL (Data Definition Language)

Theory: Used to define database schema:

• CREATE – Create tables or databases


• ALTER – Modify existing tables

1
• DROP – Delete tables/databases

Example:

CREATE TABLE employees (


id INT,
name VARCHAR(50)
);

Interview Q&A:

• Q: What does DROP do?


• A: It permanently deletes a table or database.

4. DML (Data Manipulation Language)

Theory: Used to manipulate data:

• INSERT – Add records


• UPDATE – Modify existing records
• DELETE – Remove records

Example:

INSERT INTO employees VALUES (1, 'John');


UPDATE employees SET name = 'Johnny' WHERE id = 1;
DELETE FROM employees WHERE id = 1;

Interview Q&A:

• Q: Can we undo a DELETE?


• A: Only if inside a transaction or if backup exists.

5. DQL (Data Query Language)

Theory: SELECT is used to retrieve data.

Example:

SELECT name FROM employees WHERE id = 1;

Interview Q&A:

• Q: What does WHERE do?


• A: Filters records based on a condition.

2
6. SQL Functions

Theory: Used to perform calculations:

• COUNT() – Number of records


• SUM() – Sum of values
• AVG() – Average
• MAX() / MIN() – Highest / Lowest value

Example:

SELECT COUNT(*) FROM employees;

Interview Q&A:

• Q: How to get the highest salary?


• A: SELECT MAX(salary) FROM employees;

7. GROUP BY & HAVING

Theory: GROUP BY is used with aggregate functions. HAVING is used to filter groups.

Example:

SELECT department, COUNT(*) FROM employees GROUP BY department HAVING


COUNT(*) > 5;

Interview Q&A:

• Q: Difference between WHERE and HAVING ?


• A: WHERE filters rows, HAVING filters groups.

8. Joins

Theory: Used to combine data from multiple tables:

• INNER JOIN – Common records


• LEFT JOIN – All from left + matched from right
• RIGHT JOIN – All from right + matched from left
• FULL JOIN – All records

Example:

3
SELECT a.name, b.salary
FROM employees a
INNER JOIN payroll b ON a.id = b.emp_id;

Interview Q&A:

• Q: What is the difference between INNER and LEFT JOIN?


• A: INNER returns matched only; LEFT includes all from left table.

9. Subqueries

Theory: A query inside another query.

Example:

SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);

Interview Q&A:

• Q: What are correlated subqueries?


• A: Subqueries that reference outer query columns.

10. Constraints

Theory: Used to apply rules:

• PRIMARY KEY , FOREIGN KEY


• NOT NULL , UNIQUE , CHECK

Example:

CREATE TABLE employees (


id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE
);

Interview Q&A:

• Q: Can a table have multiple UNIQUE columns?


• A: Yes.

4
11. Indexes

Theory: Indexes improve query speed.

Example:

CREATE INDEX idx_name ON employees(name);

Interview Q&A:

• Q: Do indexes slow down any operations?


• A: Yes, INSERT , UPDATE , DELETE may be slower.

12. Normalization

Theory: Process of organizing data:

• 1NF – Atomic columns


• 2NF – No partial dependencies
• 3NF – No transitive dependencies

Interview Q&A:

• Q: Why normalize data?


• A: To reduce redundancy and improve consistency.

13. Views

Theory: Virtual table based on a query.

Example:

CREATE VIEW active_employees AS SELECT * FROM employees WHERE active = 1;

Interview Q&A:

• Q: Can we update data using a view?


• A: Yes, if view is updatable.

14. Stored Procedures & Triggers

Theory:

• Stored Procedure – A saved set of SQL commands.


• Trigger – Auto-executed on certain DB events.

5
Example:

CREATE PROCEDURE GetEmployees AS BEGIN SELECT * FROM employees; END;

Interview Q&A:

• Q: What is a trigger?
• A: A function that runs automatically after a specified DB event.

15. Common Interview Questions

1. What is a primary key?


2. Difference between DELETE and TRUNCATE ?
3. Explain different types of joins.
4. How do you get the second highest salary?

SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);

1. What is a foreign key?


2. What is normalization and its types?
3. What are indexes?
4. What is a view?
5. What is a subquery?
6. Difference between WHERE and HAVING?

All the Best for Your Interview! ✨

You might also like