What is SQL?
What are the different types of SQL statements?
DDL, DML, DCL, TCL
What is the difference between WHERE and HAVING?
What is a primary key?
What is a foreign key?
What is the difference between INNER JOIN and LEFT JOIN?
What is the difference between DELETE, TRUNCATE, and DROP?
What is normalization? Explain its types.
What is denormalization?
What are constraints in SQL?
NOT NULL, UNIQUE, CHECK, etc.
Intermediate SQL Interview Questions
What is a subquery? How is it different from a JOIN?
What are indexes? What types are there?
What are aggregate functions in SQL?
SUM(), AVG(), COUNT(), etc.
Explain GROUP BY and ORDER BY.
What is the difference between UNION and UNION ALL?
What are views? How are they useful?
What is a stored procedure?
What is a trigger in SQL?
Explain window functions with examples.
What is the difference between correlated and non-correlated subqueries?
Advanced SQL Interview Questions
How do you optimize SQL queries for performance?
What is a CTE (Common Table Expression)? How is it used?
Explain indexing strategies.
How does SQL handle transactions?
ACID properties
What is a deadlock in SQL? How do you resolve it?
What are recursive queries? Give an example.
Explain RANK(), DENSE_RANK(), and ROW_NUMBER().
What is the difference between EXISTS and IN?
What does EXPLAIN PLAN do?
What is the difference between horizontal and vertical partitioning?
Practical Scenario-Based Questions
Write a SQL query to find the second highest salary.
Write a query to find duplicate records in a table.
Write a query to transpose rows to columns.
How do you update one table based on data from another table?
How do you retrieve the Nth highest value from a column?
What is SQL?
SQL (Structured Query Language) is a standard programming language used to communicate with and manage data in relational database management systems (RDBMS) like MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.
What SQL is used for:
Query data from a database (
SELECT
)Insert new records (
INSERT
)Update existing records (
UPDATE
)Delete records (
DELETE
)Create and modify database structures like tables and indexes (
CREATE
,ALTER
,DROP
)Control access to data (
GRANT
,REVOKE
)
What are the different types of SQL statements?
1. Data Definition Language (DDL)
These statements define the structure of the database objects like tables, indexes, and views.
Statements Purpose
CREATE Creates a new table, view, or database
ALTER Modifies an existing object
DROP Deletes an object
TRUNCATE Removes all records from a table
RENAME Renames a database object
2. Data Manipulation Language (DML)
These statements are used to manipulate data within tables.
Statements Purpose
SELECT Retrieves data
INSERT Adds new records
UPDATE Modifies existing records
DELETE Removes records
3. Data Control Language (DCL)
These statements manage access to the data in the database.
Statements Purpose
GRANT Gives user access privileges
REVOKE Withdraws access privileges
4. Transaction Control Language (TCL)
These statements manage changes made by DML statements and control transaction behavior.
Statements Purpose
COMMIT Saves all changes made in the transaction
ROOLBACK Undoes changes if something goes wrong
SAVEPOINT Sets a point to roll back to later
SET TRANSACTION Defines transaction properties
5. Data Query Language (DQL)
Some categorize SELECT
separately as DQL since it's used strictly for querying (not changing) data.
Statements Purpose
SELECT Retrieves data from tables
What is the difference between WHERE and HAVING?
WHERE
Filters rows before any grouping or aggregation.
Used with SELECT, UPDATE, DELETE statements.
Cannot be used with aggregate functions (like
SUM()
,AVG()
, etc.)
Example:
Filters individual rows where department = 'Sales'
.
HAVING
Filters groups after aggregation (
GROUP BY
).Used only with
GROUP BY
or aggregate functions.Can use aggregate functions like
COUNT()
,SUM()
.
Example:
Key Characteristics of a Primary Key:
Uniqueness: Each value must be unique across the table.
Not Null: A primary key column cannot contain NULL values.
Only one per table: A table can have only one primary key, but it can consist of multiple columns (called a composite key).
Example:
No comments:
Post a Comment