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

Beginner to Advanced SQL Questions Guide

Uploaded by

TARIQ ALI
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)
18 views6 pages

Beginner to Advanced SQL Questions Guide

Uploaded by

TARIQ ALI
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

✅ Beginner-Level SQL Questions

1. What is SQL?

SQL (Structured Query Language) is a standard language for storing, manipulating, and
retrieving data in relational databases.

2. What is the difference between SQL and MySQL?

• SQL → Language for managing databases.


• MySQL → A database management system (DBMS) that uses SQL.

3. What are the different types of SQL statements?

• DDL (Data Definition Language) → CREATE, ALTER, DROP, TRUNCATE


• DML (Data Manipulation Language) → INSERT, UPDATE, DELETE
• DQL (Data Query Language) → SELECT
• DCL (Data Control Language) → GRANT, REVOKE
• TCL (Transaction Control Language) → COMMIT, ROLLBACK, SAVEPOINT

4. What is the difference between CHAR and VARCHAR?

• CHAR(n) → Fixed length, always uses n bytes (padded with spaces).


• VARCHAR(n) → Variable length, uses only the required space.

5. What are primary keys and foreign keys?

• Primary Key → Uniquely identifies each record (cannot be NULL).


• Foreign Key → Establishes relationship between two tables (references primary key of
another table).

6. What is the difference between WHERE and HAVING?


• WHERE → Filters rows before grouping.
• HAVING → Filters groups after aggregation.

7. What is the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and
FULL JOIN?

• INNER JOIN → Returns matching rows from both tables.


• LEFT JOIN → All rows from left + matching rows from right.
• RIGHT JOIN → All rows from right + matching rows from left.
• FULL JOIN → All rows from both tables (matches + non-matches).

8. What is the difference between DELETE, TRUNCATE, and DROP?

• DELETE → Removes specific rows, can use WHERE. (Rollback possible)


• TRUNCATE → Removes all rows, resets identity. (Cannot rollback easily)
• DROP → Deletes the entire table (structure + data).

9. What are constraints in SQL?

Rules applied to columns:

• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT

10. What is normalization? Explain its types.

Normalization organizes data to reduce redundancy.

• 1NF → Atomic values, no repeating groups.


• 2NF → 1NF + no partial dependency.
• 3NF → 2NF + no transitive dependency.
• BCNF → Stronger form of 3NF.
✅ Intermediate-Level SQL Questions

1. What is the difference between UNION and UNION ALL?

• UNION → Combines results, removes duplicates.


• UNION ALL → Combines results, keeps duplicates.

2. Explain indexes in SQL. What are clustered and non-clustered indexes?

• Index improves query performance by speeding up lookups.


• Clustered Index → Sorts and stores rows physically (1 per table).
• Non-clustered Index → Separate structure pointing to data (many per table).

3. What is a subquery? What types are there?

A query inside another query.

• Single-row subquery → Returns one result.


• Multi-row subquery → Returns multiple results.
• Correlated subquery → Depends on outer query.
• Nested subquery → Independent inner query.

4. What is the difference between correlated and non-correlated subqueries?

• Non-correlated → Executes once, result reused.


• Correlated → Executes for each row of outer query.

5. Explain ACID properties in databases.

• Atomicity → All or nothing.


• Consistency → Maintain valid state.
• Isolation → Transactions don’t interfere.
• Durability → Changes persist even after crash.
6. What are transactions in SQL?

A transaction is a sequence of SQL statements executed as a single unit. Controlled with


BEGIN, COMMIT, ROLLBACK.

7. What is the difference between RANK(), DENSE_RANK(), and


ROW_NUMBER()?

• ROW_NUMBER() → Sequential, no duplicates.


• RANK() → Duplicates get same rank, gaps remain.
• DENSE_RANK() → Duplicates same rank, no gaps.

8. What is a stored procedure? How is it different from a function?

• Stored Procedure → Precompiled set of SQL statements, can return multiple values,
supports transactions.
• Function → Returns a single value, can be used in queries.

9. What are views in SQL? Why are they used?

A view is a virtual table based on query results.


Used for: security, simplifying queries, abstraction.

10. What are triggers in SQL?

Triggers are automated actions executed when an event (INSERT, UPDATE, DELETE) occurs on a
table.

✅ Advanced-Level SQL Questions

1. What are CTEs (Common Table Expressions)?

Temporary named result sets used within a query.

WITH Sales_CTE AS (
SELECT product_id, SUM(amount) AS total
FROM sales GROUP BY product_id
)
SELECT * FROM Sales_CTE WHERE total > 1000;

2. Explain window functions in SQL.

Functions that perform calculations over a set of rows related to the current row, without
collapsing them.
Examples: ROW_NUMBER(), RANK(), LEAD(), LAG(), SUM() OVER().

3. What is the difference between OLTP and OLAP systems?

• OLTP (Online Transaction Processing) → Fast inserts/updates, small transactions


(banking, e-commerce).
• OLAP (Online Analytical Processing) → Complex queries, aggregates, large-scale
reporting (data warehouses).

4. What are materialized views?

Stored query results (unlike normal views). Can be refreshed periodically. Improve performance
in reporting/analytics.

5. How do you optimize a slow SQL query?

• Use proper indexes


• Avoid SELECT *
• Use joins instead of subqueries when possible
• Analyze execution plan
• Partition large tables
• Optimize WHERE conditions

6. What is sharding and partitioning in databases?

• Partitioning → Splitting a table into smaller parts within the same DB.
• Sharding → Distributing data across multiple databases/servers.
7. Explain deadlocks in SQL. How can you prevent them?

• Deadlock → Two transactions wait for each other, causing a lock.


• Prevention:
o Keep transactions short
o Access resources in same order
o Use proper indexing

8. What is indexing strategy, and how do you decide which columns to index?

• Index frequently queried columns.


• Use indexes on WHERE, JOIN, ORDER BY columns.
• Avoid indexing small tables or columns with high updates.
• Prefer composite indexes for multiple conditions.

9. How do you implement database security in SQL?

• Use user roles and privileges


• Encrypt sensitive data
• Enable TLS for connections
• Use strong passwords
• Prevent SQL injection with parameterized queries

10. Compare SQL with NoSQL databases.

• SQL → Structured, fixed schema, ACID compliant, good for transactions.


• NoSQL → Schema-less, scalable, handles unstructured data, BASE model.

You might also like