DEEKSHITHA TECH TALKS
SQL MOST ASKED INTERVIEW QUESTIONS ENTO CHUSEDDAM MITRONS 😊
ELANTIVI MEEKU INKA KAVALI ANTE MANA PAGE NI FOLLOW AVVADAM MATRAM
MARCHIPOKANDI <3
1) What is SQL?
A) SQL (Structured Query Language) is a standard language used to manage and
manipulate relational databases. It allows users to create, read, update, and delete data
using queries.
2)What is Primary Key?
A Primary Key is a unique identifier for a record in a database table. It ensures that each row
has a distinct value and cannot contain NULL values.
3)
What is Foreign Key?
A Foreign Key is a column (or set of columns) in one table that establishes a link to the
Primary Key in another table. It ensures referential integrity by enforcing relationships
between tables.
4)What are constraints in SQL?
Constraints in SQL are rules applied to table columns to enforce data integrity and
consistency. Common constraints include:
• PRIMARY KEY – Ensures each record is unique and non-null.
• FOREIGN KEY – Maintains referential integrity between tables.
• NOT NULL – Prevents a column from having NULL values.
• UNIQUE – Ensures all values in a column are distinct.
• CHECK – Restricts values based on a condition.
• DEFAULT – Assigns a default value if no value is provided.
5)Write a query to retrieve all records from a table named employees?
SELECT * FROM employees;
This will return all columns and rows from the employees table.
6)
What is the difference between DELETE and TRUNCATE?
DELETE removes specific rows based on a condition and can be rolled back
TRUNCATE removes all rows instantly, resets identity columns, and cannot be rolled back in
most databases.
7)How do you find the maximum salary from an employees table?
SELECT MAX(salary) AS max_salary FROM employees;
8) Write a query to fetch the second-highest salary from the employees table?
SELECT MAX(salary) AS second_highest_salary FROM employees WHERE salary < (SELECT
MAX(salary) FROM employees);
9) What is a JOIN Explain its types?
A JOIN in SQL is used to combine rows from two or more tables based on a related column.
Types of JOINs:
1. INNER JOIN – Returns only matching records from both tables.
2. LEFT JOIN (LEFT OUTER JOIN) – Returns all records from the left table and matching
records from the right table; fills non-matching rows with NULLs.
3. RIGHT JOIN (RIGHT OUTER JOIN) – Returns all records from the right table and
matching records from the left table; fills non-matching rows with NULLs.
4. FULL JOIN (FULL OUTER JOIN) – Returns all records from both tables, filling
NULLs where there’s no match.
5. CROSS JOIN – Returns the Cartesian product of both tables (every row from table1
is paired with every row from table2).
6. SELF JOIN – Joins a table to itself based on a related column.
10) Write a query to fetch employees names and department names using JOIN?
SELECT employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
11) What is a GROUP BY clause in SQL?
The GROUP BY clause in SQL is used to group rows that have the same values in specified
columns into summary rows, often used with aggregate functions like COUNT(), SUM(),
AVG(), MAX(), or MIN() to perform calculations on each group.
12) Write a query to count employees in each department?
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
13) What is the difference between WHERE and HAVING clauses?
WHERE is used to filter records before any grouping is done (i.e., filters individual rows in
the table).
HAVING is used to filter records after the grouping is done (i.e., filters groups created by
GROUP BY).
14) Write a query to fetch departments with more than 5 employees?
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;
15) Explain UNION and UNION ALL?
UNION: Removes duplicates and returns only unique results.
UNION ALL: Returns all results, including duplicates.
16) What is a subquery in SQL?
A subquery in SQL is a query nested inside another query. It is used to retrieve data that will
be used by the main query. Subqueries can be placed inside SELECT, FROM, or WHERE
clauses.
17) Write a query to find all employees whose salary is greater than the average salary?
SELECT * FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
18) What is the difference between INNER JOIN and OUTER JOIN?
INNER JOIN: Returns only the matching records from both tables.
OUTER JOIN: Returns matching records plus unmatched records from one or both tables.
• Types:
o LEFT JOIN: Returns all employees, even if they don’t have a department.
o RIGHT JOIN: Returns all departments, even if they don’t have employees.
o FULL JOIN: Returns all employees and all departments, filling unmatched
rows with NULL.
19) Write a query to fetch the current date in SQL?
SELECT CURDATE();
To fetch the current date in SQL, you can use the following queries depending on the
database system:
20) What is indexing in SQL?
Indexing in SQL is a technique used to improve the speed of data retrieval from a database
table. It works like a book index, allowing the database to find rows faster instead of
scanning the entire table.
21) What is normalization? Explain its types(1NF,2NF,3NF,BCNF)?
Normalization is the process of organizing a database to reduce redundancy and improve data
integrity by dividing tables into smaller, related tables.
• 1NF: Ensures atomic values (no multiple values in a column).
• 2NF: Removes partial dependencies (columns must depend on the whole primary
key).
• 3NF: Removes transitive dependencies (non-key columns should not depend on other
non-key columns).
• BCNF: A stricter form of 3NF ensuring every determinant is a candidate key.
22) What is denormalization?
Denormalization is the process of combining normalized tables to improve query
performance by reducing joins. It introduces some redundancy to speed up data retrieval at
the cost of storage efficiency.
23) Write a query to add a new column email to the employees table?
ALTER TABLE employees ADD COLUMN email VARCHAR(255);
24) What is stored procedure on SQL?
A stored procedure in SQL is a precompiled collection of SQL statements that can be
executed on the database. It is stored in the database and can be called to perform a specific
task, such as inserting, updating, or deleting data, or retrieving results.
25) Write a basic stored procedure to fetch all employees?
CREATE PROCEDURE GetAllEmployees()
BEGIN
SELECT * FROM employees;
END;
26)What are triggers in SQL?
A trigger in SQL is a special stored procedure that automatically executes in response to
specific events (INSERT, UPDATE, DELETE) on a table. Triggers help enforce business
rules, maintain audit logs, and automate tasks.
27)What is VIEW in SQL?
A view in SQL is a virtual table based on a SELECT query that dynamically presents data
from one or more tables without storing it physically. It simplifies complex queries, enhances
security, and provides a consistent data representation.
28)Write a query to create a view for employees with salary greater than 50,000?
CREATE VIEW HighSalaryEmployees AS
SELECT * FROM Employees
WHERE Salary > 50000;
29)What is ACID PROPERTIES ?
ACID properties in SQL ensure reliable database transactions:
1. Atomicity – A transaction is all or nothing; if one part fails, the entire transaction is
rolled back.
2. Consistency – Transactions bring the database from one valid state to another,
maintaining integrity.
3. Isolation – Concurrent transactions execute independently without interference.
4. Durability – Once a transaction is committed, it remains permanent even in case of
system failure.
30)What is an aggregate function? Provide Example?
An aggregate function in SQL performs a calculation on a set of values and returns a single
result. It is commonly used with the GROUP BY clause.
Common Aggregate Functions:
• SUM() – Calculates the total sum
• AVG() – Finds the average value
• COUNT() – Counts the number of records
• MAX() – Finds the maximum value
• MIN() – Finds the minimum value
31)Explain NULL values in SQL?
In SQL, a NULL value represents missing or unknown data in a table. It is not the same as
an empty string ('') or zero (0).