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

SQL Interview

SQL

Uploaded by

bisht3.dheeraj
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)
9 views5 pages

SQL Interview

SQL

Uploaded by

bisht3.dheeraj
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

​1.

SQL Basics (must master)​


CREATE, ALTER, DROP, TRUNCATE​
​●​ ​Data Definition (DDL):​​ ​.​

INSERT, UPDATE, DELETE​


​●​ ​Data Manipulation (DML):​​ ​.​

SELECT​
​●​ ​Data Query (DQL):​​ ​.​

GRANT, REVOKE​
​●​ ​Data Control (DCL):​​ ​.​

COMMIT, ROLLBACK, SAVEPOINT​


​●​ ​Transaction Control (TCL):​​ ​.​

🔹 2. Joins (Banking Examples)​



-- Get all customers with their account balances​

SELECT c.customer_id, [Link], [Link]​


FROM Customers c​

INNER JOIN Accounts a ON c.customer_id = a.customer_id;​


​●​ ​INNER JOIN​​→ Matching rows.​

​●​ ​LEFT JOIN​​→ All from left + matches.​

​●​ ​RIGHT JOIN​​→ All from right + matches.​

​●​ ​FULL JOIN​​→ All rows from both.​

​●​ ​SELF JOIN​​→ Joining a table to itself (e.g., parent-child​​accounts).​

🔹 3. Subqueries​

-- Get customers with balance greater than avg balance​

SELECT customer_id, name​



FROM Accounts​

WHERE balance > (SELECT AVG(balance) FROM Accounts);​


​●​ ​Scalar subquery (returns single value).​

​●​ ​Correlated subquery (depends on outer query).​

🔹
​ 4. Window Functions (very common in banking​
​reports)​
-- Rank customers by balance​

SELECT customer_id, balance,​


RANK() OVER (ORDER BY balance DESC) AS rank​


FROM Accounts;​

​●​ ​
ROW_NUMBER()​​→ Unique sequence.​

​●​ ​
RANK()​​→ Gaps in ranking.​

​●​ ​
DENSE_RANK()​​→ No gaps.​

​●​ ​
SUM() OVER(PARTITION BY …)​​→ Running totals.​

🔹 5. Common Table Expressions (CTE)​



WITH HighValueCustomers AS (​

SELECT customer_id, balance​


FROM Accounts​

WHERE balance > 1000000​



)​

SELECT * FROM HighValueCustomers;​


✅ Useful for complex banking queries (audit reports, reconciliation).​


🔹 6. Indexing​

​●​ ​Clustered Index​​→ Reorders table data (one per table).​

​●​ ​Non-Clustered Index​​→ Creates separate structure (multiple​​allowed).​

WHERE​
​●​ ​Use indexes for​​ JOIN​
​,​​ GROUP BY​​columns.​
​,​​
⚠ INSERT/UPDATE/DELETE​
​ Over-indexing → Slows down​​ ​.​

🔹 7. Transactions (Critical in Banking)​



BEGIN;​

UPDATE Accounts SET balance = balance - 5000 WHERE id = 1;​


UPDATE Accounts SET balance = balance + 5000 WHERE id = 2;​


COMMIT;​

​●​ ​ACID properties:​​Atomicity, Consistency, Isolation,​​Durability.​

​●​ ​Isolation Levels:​

​○​ ​Read Uncommitted (dirty reads).​

​○​ ​Read Committed (default in Oracle/SQL Server).​

​○​ ​Repeatable Read.​

​○​ ​Serializable.​
🔹 8. Performance Tuning​

EXPLAIN PLAN​​or​​
​●​ ​Use​​ EXPLAIN​​(MySQL) to analyze queries.​

​●​ ​Replace subqueries with joins if possible.​

​●​ ​Partition large tables.​

​●​ ​Use proper indexes (covering indexes).​

WHERE​​clauses (e.g.,​​
​●​ ​Avoid functions in​​ WHERE YEAR(date)​​
= 2025​
​).​

📝 SQL Interview Q&A (Banking Focus)​



​Q1.​​How would you find duplicate transactions in a​​table?​

SELECT transaction_id, COUNT(*)​


FROM Transactions​

GROUP BY transaction_id​

HAVING COUNT(*) > 1;​


​Q2.​​Write a query to find 2nd highest salary from​​Employees.​

SELECT MAX(salary)​

FROM Employees​

WHERE salary < (SELECT MAX(salary) FROM Employees);​


DENSE_RANK()​​with window functions).​


​(or use​​

​Q3.​​Query to find accounts with no transactions in​​last 30​

​Unlock more with Plus​

You might also like