Complete SQL Testing Guide:
Queries, JOINs, Nth Salary Logic & DB Verification
Master database testing for QA Engineers and SDETs. Learn SQL SELECT queries, INNER/LEFT/RIGHT JOINs, GROUP BY aggregations, Nth highest salary queries, window functions ('DENSE_RANK()'), and JDBC Java automation with 52+ interview Q&As.
How SQL Databases Work in Plain English & Hinglish (Student Guide)
A relational database is like a digital filing cabinet or multi-sheet Excel workbook. Here is the step-by-step breakdown for beginners:
1. Excel Workbook Analogy
English: Think of a Database as an Excel Workbook. A Table is an Excel Sheet; a Row is a single record (User #101); a Column is an attribute (Email, Age); and a Primary Key is the unique ID (Aadhaar/SSN).
2. Hinglish Explanation
Hinglish: SQL Testing ka matlab hai Backend Data Integrity Verify Karna! Jaise Amazon app par order place hone par check karna ki Database ke orders table mein order entry exact price and status ke saath save hui ya nahi!
3. Why Testers Need SQL
QA Role: 70% of production defects stem from corrupted database states or broken foreign key relationships. Testing UI without SQL verification is incomplete!
Step-by-Step SQL Query Execution Lifecycle
(Sends SQL Query Text)
(Optimizes Index & Execution Plan)
(Returns Rows & Status to QA)
5-Step SQL Testing & Database Path for SDETs
Master SQL step-by-step: SELECT & Filtering → SQL JOINs → Aggregates & Grouping → Window Functions & Nth Salary → DB Testing & JDBC Automation.
Step 1: SELECT & Filtering
Master basic SELECT statements, WHERE clauses, LIKE wildcard patterns, IN, and BETWEEN conditions.
Study Basic Queries →Step 2: SQL JOINs
Learn INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN across multiple relational tables.
Master SQL JOINs →Step 3: Aggregates & Grouping
Understand GROUP BY vs HAVING clauses, COUNT(), SUM(), AVG(), MIN(), and MAX() aggregate functions.
Study Aggregations →Step 4: Nth Salary & Windows
Master correlated subqueries, DENSE_RANK(), ROW_NUMBER(), and Nth highest salary queries.
Step 5: DB QA & JDBC Automation
Automate database verification using Java JDBC drivers, ResultSet assertions, and TestNG.
Automate DB Testing →๐ ๏ธ Interactive SQL Learner & Developer Suite
Interactive SQL Query Builder & Sandbox
Select a Table, Filter Condition, and Order By rule below to generate production SQL SELECT queries live.
Visual SQL JOIN Venn Diagram & Code Generator
Click a JOIN type to view its mathematical set theory logic and exact SQL query syntax.
Nth Highest Salary Query Calculator & Logic Visualizer
Select Nth rank (2nd, 3rd, 5th highest salary) to inspect 4 different SQL solutions asked in interviews!
LIMIT OFFSET, and DENSE_RANK() window functions!
Timed SQL for QA & Database Quiz
Test your real-time knowledge on SQL JOINs, GROUP BY vs HAVING, Subqueries, and duplicate record removal.
Interactive ER Diagram & Table Schema Visualizer
Inspect database schemas, Primary Keys (PK), Foreign Keys (FK), and sample data across core relational tables.
Query Execution Plan & Index Performance Analyzer
Compare Full Table Scans vs Index Seeks to understand how B-Tree indexes speed up query performance by 100x.
ACID Transactions & Concurrency Simulator
Simulate real-world banking transfers with BEGIN TRANSACTION, COMMIT, and ROLLBACK to master Atomicity & Isolation.
Account A (Sender)
$2,500
Committed StateAccount B (Receiver)
$1,000
Committed StateTransaction Ledger State
IDLE
No active transactionWindow Functions Interactive Playground
Compare ROW_NUMBER(), RANK(), DENSE_RANK(), and LEAD/LAG over partitioned dataset with tied values.
Database Testing & ETL QA Readiness Evaluator
Assess your proficiency across DDL/DML, Joins, Aggregation, Subqueries, Window Functions, and Data Verification.
Your Calculated SQL Readiness Score
Evaluating proficiency...
1-Click Advanced SQL Interview Cheat Sheet Exporter
Select your target SQL topics to generate a formatted code cheat sheet for last-minute revision.
Module 1: SQL Foundation (SELECT & Filters) 20 Q&As
Click to Expand/Collapse1. Which SQL command is used to retrieve data from a database?
Hinglish: Database se data nikalne ke liye SELECT command use karte hain.
Example:
SELECT * FROM Customers;
2. Which SQL command is used to add new rows of data to a table?
Hinglish: Table mein naya data daalne ke liye INSERT INTO use karte hain.
Example:
INSERT INTO Products (Name, Price) VALUES ('Laptop', 1200);
3. Which SQL command is used to modify existing data in a table?
Hinglish: Table mein maujood data ko badalne ke liye UPDATE use karte hain.
Example:
UPDATE Customers SET City = 'New York' WHERE CustomerID = 1;
4. Which SQL command is used to delete rows of data from a table?
Hinglish: Table se data ki rows mitane ke liye DELETE FROM use karte hain.
Example:
DELETE FROM Orders WHERE OrderID = 101;
5. What is the difference between DELETE and TRUNCATE?
Hinglish: DELETE row-by-row data hatata hai aur undo ho sakta hai. TRUNCATE pura table turant khali kar deta hai aur fast hota hai par wapas nahi aa sakta.
6. What is the difference between DELETE and DROP?
Hinglish: DELETE data hatata hai par table rehta hai; DROP table aur uske data dono ko khatam kar deta hai.
7. What is a Primary Key?
Hinglish: Table ki har row ki ek unique ID jo kabhi khali (NULL) nahi ho sakti.
8. What is a Foreign Key?
Hinglish: Do tables ko jodne waali key jo doosri table mein primary key hoti hai.
9. What is the purpose of the DISTINCT keyword?
Hinglish: Query results mein se duplicates hata kar sirf unique values dikhane ke liye.
10. Explain the 'LIKE' operator and wildcards (% and _).
Hinglish: Text patterns dhoondhne ke liye. % matlab kai characters aur _ matlab sirf ek character.
11. What is the BETWEEN operator used for?
Hinglish: Do limits ke beech ki values ko select karne ke liye. Example:
WHERE Price BETWEEN 10 AND 20;
4. What is a "Constraint" in SQL?
Hinglish: Columns pe lagaye gaye rules taaki galat data na aaye.
5. How to check for NULL values in SQL?
IS NULL or IS NOT NULL instead of = NULL.Hinglish: Khali values dhoondhne ke liye
IS NULL use hota hai.
6. What is the difference between a Table and a View?
Hinglish: Table mein data physically hota hai, View sirf ek 'khidki' (virtual table) hai.
7. What is the purpose of the COALESCE function?
Hinglish: List mein se pehli 'non-null' value nikaalne ke liye.
9. What is an Auto-Increment field?
Hinglish: Nayi row aate hi ID apne aap 1, 2, 3... badhne wala system.
10. What is the difference between Character and Varchar?
Hinglish: CHAR ki length fix hoti hai, VARCHAR jitna text ho utni hi jagah leta hai.
11. What is a Composite Key?
Hinglish: Jab do ya zyada columns milkar ek unique ID banayein.
12. How to delete all records but keep the table structure?
TRUNCATE TABLE tablename;Hinglish: Table structure bachate hue saara data saaf karne ke liye TRUNCATE use karein.
13. What is the default port for MySQL and PostgreSQL?
Hinglish: MySQL 3306 use karta hai aur PostgreSQL 5432.
Module 2: SQL Joins & Relationships 7 Q&As
Click to Expand/Collapse1. What is an INNER JOIN? Give a syntax and output example.
Hinglish: INNER JOIN dono tables mein se sirf wahi rows dikhata hai jo aapas mein match karti hon. Agar koi match nahi hai, to wo data result mein nahi aayega.
Query:
SELECT Users.Name, Orders.OrderID, Orders.Amount
FROM Users
INNER JOIN Orders ON Users.UserID = Orders.UserID;
Result Output:
+-------+---------+--------+
| Name | OrderID | Amount |
+-------+---------+--------+
| Ram | 101 | 500 |
| Priya | 102 | 300 |
+-------+---------+--------+
2. What is a LEFT (OUTER) JOIN? Give a syntax and output example.
Hinglish: LEFT JOIN left side waali table ka saara data dikhayega, aur right side waali table se matching data laayega. Agar match nahi hoga, to right side ke columns mein NULL dikhayega.
Query:
SELECT Users.Name, Orders.OrderID, Orders.Amount
FROM Users
LEFT JOIN Orders ON Users.UserID = Orders.UserID;
Result Output:
+-------+---------+--------+
| Name | OrderID | Amount |
+-------+---------+--------+
| Ram | 101 | 500 |
| John | NULL | NULL | (No order matched for John)
| Priya | 102 | 300 |
+-------+---------+--------+
3. What is a RIGHT (OUTER) JOIN? Give a syntax and output example.
Hinglish: RIGHT JOIN right side waali table ka saara data dikhayega, aur left table se matching data laayega. Agar match nahi hoga, to left table ke columns mein NULL dikhayega.
Query:
SELECT Users.Name, Orders.OrderID, Orders.Amount
FROM Users
RIGHT JOIN Orders ON Users.UserID = Orders.UserID;
Result Output:
+-------+---------+--------+
| Name | OrderID | Amount |
+-------+---------+--------+
| Ram | 101 | 500 |
| Priya | 102 | 300 |
| NULL | 103 | 150 | (OrderID 103 has UserID 9, which isn't in Users)
+-------+---------+--------+
4. What is a FULL (OUTER) JOIN? Give a syntax and output example.
Hinglish: FULL JOIN dono tables ka poora ka poora data dikhayega. Jahan matches milenge wahan connect karega, jahan nahi milenge wahan nulls daal dega.
Query:
SELECT Users.Name, Orders.OrderID, Orders.Amount
FROM Users
FULL OUTER JOIN Orders ON Users.UserID = Orders.UserID;
Result Output:
+-------+---------+--------+
| Name | OrderID | Amount |
+-------+---------+--------+
| Ram | 101 | 500 |
| John | NULL | NULL |
| Priya | 102 | 300 |
| NULL | 103 | 150 |
+-------+---------+--------+
5. What is a SELF JOIN? Provide a real-world hierarchy database scenario.
Hinglish: Jab kisi table ko uski khud ki table se join kiya jaaye, to use SELF JOIN bolte hain. Jaise employee table mein manager aur employee ki data hierarchy nikalne ke liye.
Example Query (Employee & Manager):
SELECT e.Name AS Employee, m.Name AS Manager
FROM Employees e
LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID;
6. What is a CROSS JOIN? When do QA engineers use it?
Hinglish: CROSS JOIN Cartesian product dikhata hai. Pehli table ki har row doosri table ki har row se multiply hoti hai. Isme ON condition nahi hoti.
QA Use Case: Test Data Generation. For example, if you have a table of Sizes (S, M, L) and a table of Colors (Red, Blue, Green), a CROSS JOIN will generate all possible product combinations for QA testing verification.
8. Difference between UNION and UNION ALL?
Hinglish: UNION unique results deta hai, UNION ALL sab kuch (duplicates samet).
Module 3: Aggregates & Grouping (GROUP BY vs HAVING) 3 Q&As
Click to Expand/Collapse1. Name the common SQL Aggregate Functions.
Hinglish: Calculations ke functions: SUM, AVG, COUNT, MIN, MAX.
2. What is the purpose of GROUP BY?
Hinglish: Ek jaise data wali rows ko ek group mein ikatha karne ke liye.
3. Difference between WHERE and HAVING clause?
Hinglish: WHERE normal records pe chalta hai, aur HAVING tab jab results group ho chuke hon.
Module 4: Advanced SQL & Nth Salary Window Functions 14 Q&As
Click to Expand/Collapse11. What is Query Optimization?
Hinglish: Query ko fast banane ka tareeka (jaise index use karna).
1. What is a Subquery?
Hinglish: Ek query ke andar doosri query.
2. Describe ACID properties in a Database.
Hinglish: Database transactions ke 4 main rules: Atomicity, Consistency, Isolation, Durability.
3. What is an Index and why is it used?
Hinglish: Database se data jaldi dhoondhne ke liye banaya gaya pointer.
4. What is Database Normalization?
Hinglish: Tables ko aise design karna taaki data faltu repeats na ho (Duplicates kam karna).
5. Difference between View and Materialized View?
Hinglish: View sirf save ki gayi query hai; Materialized view us query ka data bhi store karta hai taaki processing fast ho.
6. What are Stored Procedures?
Hinglish: Save kiya hua SQL code jise baar-baar run kiya ja sakta hai.
7. What is Database Trigger?
Hinglish: Table mein kuch hone par (เคเฅเคธเฅ insert) apne aap piche chalne wala code.
9. What is COMMIT and ROLLBACK?
Hinglish: COMMIT changes ko pakka (save) karta hai, ROLLBACK galti hone par purane state pe wapas le jata hai.
10. What is a Deadlock in Databases?
Hinglish: Jab do kaam ek doosre ka rasta rok kar hamesha ke liye atak jayein.
15. What is 'Dirty Read' in Databases?
Hinglish: Aisi information padhna jo badal toh gayi hai par abhi tak DB mein pakki save (commit) nahi huwi.
16. How to find the Nth highest salary? (Restored)
DENSE_RANK(). Example: SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rnk FROM Emp) WHERE rnk = N;Hinglish: DENSE_RANK() use karke har salary ko number do aur fir N number wala utha lo.
17. What are CTEs (Common Table Expressions)? (Restored)
WITH clause) that makes complex queries easier to read and maintain.Hinglish: Ek temporary result set jo lambi queries ko saaf suthra aur asaan banane ke liye use hota hai.
18. Explain ROW_NUMBER() vs RANK() vs DENSE_RANK(). (Restored)
Hinglish: Ranking dene ke 3 tareeke: ROW_NUMBER saaf ginti deta hai, RANK ties pe numbers kudata hai, DENSE_RANK gap nahi chhodta.
Module 5: Database Testing Scenarios & QA Best Practices 8 Q&As
Click to Expand/Collapse12. What is SQL Injection?
Hinglish: Hacker dwara galat SQL code daal kar database hack karne ki koshish.
13. How to prevent SQL Injection?
Hinglish: Parameterized queries aur sahi input validation use karke.
14. What is 'Data Migration Testing'?
Hinglish: Ek DB se doosre mein data shift karte waqt ye check karna ki koi galti ya loss toh nahi hua.
1. How do you test a Database Migration?
Hinglish: Dono systems mein row count aur data ki quality match karke check karna.
2. What is ETL Testing?
Hinglish: Data ko shahi tareeke se nikalne, badalne aur save karne ki testing.
3. What is Negative Testing in Databases?
Hinglish: Galat data daal kar check karna ki DB usko reject karta hai ya nahi.
8. How to find duplicate records in a table?
GROUP BY on all columns and HAVING COUNT(*) > 1.Hinglish: Group by karke jin rows ka count 1 se zyada hai, wo duplicates hain.
14. What is Data Integrity?
Hinglish: Ye dhyan rakhna ki data hamesha sahi aur constant rahe, bina kisi galti ke.