0% found this document useful (0 votes)
22 views5 pages

SQL-rdms Interview Trickyquestions

Uploaded by

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

SQL-rdms Interview Trickyquestions

Uploaded by

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

✅ Advanced / Tricky

Q9. What’s the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?


Example: marks table: [90, 90, 85]

ROW_NUMBER(): 1, 2, 3

RANK(): 1, 1, 3 (skips rank after ties)

DENSE_RANK(): 1, 1, 2 (no gap in ranking)

Q10. How do you find the 2nd highest salary from employee table?
Several ways:

-- Using LIMIT / OFFSET


SELECT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;

-- Using subquery
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

-- Using RANK()
SELECT salary FROM (
SELECT salary, RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t WHERE rnk = 2;

Q11. Tricky question: What happens if you insert NULL into a column with a UNIQUE
constraint?

Allowed: UNIQUE constraint allows multiple NULL values (since NULL ≠ NULL).

Q12. Difference between primary key and unique key?

Primary Key: only one per table, auto NOT NULL, ensures entity integrity.

Unique Key: multiple per table allowed, can contain NULLs.

Q13. Can you have a foreign key referencing a non-primary key column?

✅ Yes, as long as the referenced column has a unique constraint.

Q14. What’s the difference between clustered index vs non-clustered index in


detail?

Clustered: actual data rows sorted → only 1 allowed.

Non-clustered: separate structure → many allowed.

Q15. What is a deadlock in SQL? How to prevent?

Deadlock: two transactions waiting for each other’s locks, neither can proceed.

Prevention:

Access objects in same order


Keep transactions short

Use lower isolation level if possible

💡 Very Common Tricky Question Interviewers Ask:


👉 “If a table has a composite primary key (say (A, B)), can column A have duplicate
values?”

Answer: YES, duplicates are allowed in A as long as (A,B) pair is unique.

Do you want me to prepare this into a PDF-style 50+ question handbook (with
answers) for your SQL/RDBMS interview prep like we did for Java?

You said:
give me more intvie wquestions? keep level simple/medium and hard
ChatGPT said:

Perfect 👌 Let’s build a big collection of SQL/RDBMS interview questions across 3


levels (Simple → Medium → Hard).

I’ll keep them concise with short answers or hints so you can recall them quickly
in interviews.

📌 SQL / RDBMS Interview Questions


🔹 Simple Level

What is SQL?
Structured Query Language for managing relational databases.

What’s the difference between SQL and NoSQL?

SQL: structured, relational, schema-based.

NoSQL: unstructured/semi-structured, flexible schema.

What is the difference between DDL, DML, DCL, and TCL?

DDL – Data Definition (CREATE, ALTER, DROP).

DML – Data Manipulation (INSERT, UPDATE, DELETE).

DCL – Data Control (GRANT, REVOKE).

TCL – Transaction Control (COMMIT, ROLLBACK).

What is a primary key?


Uniquely identifies each row, cannot be null.

What is a foreign key?


References another table’s primary/unique key for referential integrity.

What is the difference between CHAR and VARCHAR?

CHAR: fixed length.

VARCHAR: variable length.

What is the default sort order in SQL?


ASC (ascending).

What’s the difference between IS NULL and = NULL?

Use IS NULL; = NULL is invalid.

🔹 Medium Level

What’s the difference between EXISTS and IN?

EXISTS: checks row existence (faster in large datasets).

IN: checks for values in a list.

What is a view? Advantages?


Virtual table from a query.

Security (hide sensitive columns).

Simplify queries.

Can a view be updated?

Yes, if it references a single base table without aggregations/joins.

Difference between COALESCE and ISNULL?

COALESCE: returns first non-null from a list (ANSI standard).

ISNULL: replaces only with given value (SQL Server specific).

What is a stored procedure vs function?

Procedure: can have multiple statements, return 0/many values, allow DML.

Function: must return a value, usually used in SELECTs.

What is a trigger?
Special procedure that runs automatically on insert/update/delete.

Difference between OLTP and OLAP?

OLTP: transactional, day-to-day operations.

OLAP: analytical, reporting.

How do you detect duplicate rows in a table?

SELECT col, COUNT(*)


FROM table
GROUP BY col
HAVING COUNT(*) > 1;

What is a surrogate key?


Artificial key (like auto-increment ID) instead of natural business key.

What is the difference between clustered and non-clustered index?


Clustered: physically orders rows.

Non-clustered: logical index pointing to rows.

What is the difference between ANY and ALL?

ANY: true if condition matches at least one.

ALL: true if condition matches all.

🔹 Hard / Tricky Level

What are isolation levels in SQL transactions?

Read Uncommitted → dirty reads allowed.

Read Committed → no dirty reads.

Repeatable Read → no dirty/non-repeatable reads.

Serializable → strictest, no phantom reads.

Explain deadlock with an example.


Two transactions holding locks and waiting for each other = deadlock.

How to find the Nth highest salary without using LIMIT or TOP?

SELECT salary
FROM employees e1
WHERE N-1 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary > e1.salary
);

What is the difference between DELETE with no WHERE and TRUNCATE?

Both remove all rows.

DELETE: slower, logged, triggers fire.

TRUNCATE: faster, minimal logging, no triggers.

What is a CTE (Common Table Expression)?


Temporary named result set, defined with WITH.
Useful for recursive queries.

What is the difference between star schema and snowflake schema?

Star: dimension tables directly linked to fact table.

Snowflake: dimensions normalized into multiple tables.

What is sharding in databases?


Splitting data horizontally across multiple servers for scalability.

How would you optimize a slow query?


Check execution plan.

Add indexes.

Rewrite query.

Partitioning.

Reduce joins/subqueries.

What’s the difference between correlated subquery and non-correlated subquery?

Non-correlated: runs independently.

Correlated: depends on outer query row.

Tricky: Can a table have multiple foreign keys referencing the same parent table?

✅ Yes, if referencing different columns.

Tricky: What happens if you group by a column but don’t include it in SELECT?

Error (most SQL engines) unless using aggregate function.

👉 This list covers 30 questions across levels.


I can expand this to 100+ with detailed answers and examples (like a PDF handbook)
if you want.

Would you like me to prepare a structured Q&A guide (50–100 questions) so you can
revise like an interview booklet?

You said:
give me mor equestions answers but befor ethat check if the queyr provided for this
question is correct ? How to find the Nth highest salary without using LIMIT or
TOP?
ChatGPT said:

Great question 👍 Let’s first carefully check the Nth highest salary query you
mentioned.

You might also like