SQL Interview Questions
2.4K subscribers
5 photos
45 links
Channel for SQL learning: @sql_res
Chennal for SQL syntax: @sql_syntax
Chennal for SQL interview questions:
@etl_sql_interview
Download Telegram
Let's learn how to build Production graded Agentic AI in most effective way, roadmap, courses, links
https://t.me/AI_Agents_RAG_Learn
Sql Interview question:
( Answers will be posted 12 hrs after the question )

1. Question:
Why does applying a function like YEAR(order_date) on an indexed column inside a WHERE clause typically lead to a non-SARGable condition and a full table scan?

A. It transforms the stored column values for every row before comparing them, preventing the query engine from navigating the pre-sorted B-Tree index structure using an index seek.

B. It violates strict ACID normalization rules, causing the transaction log to lock the entire table instead of row-level locking.

C. It converts the data type from integer to varchar, triggering a catastrophic implicit conversion error that halts query execution.

D. It disables multi-threading capabilities, forcing the query optimizer to process records on a single CPU core sequentially.
1. Answer : A. It transforms the
stored column values for every row before comparing them, preventing the query engine from navigating the pre-sorted B-Tree index structure using an index seek.

Reason: A condition is SARGable (Search Argument Able) only when the database engine can utilize an index seek directly on the raw column values stored in the B-Tree. Wrapping the column in a function alters its representation dynamically per row, necessitating a full index or table scan to compute the function value for each entry.
Sql Interview question:

2. "Your query takes 3 minutes to run on a table with 50 million rows. What do you check first?"
SQL Interview Questions
Sql Interview question: 2. "Your query takes 3 minutes to run on a table with 50 million rows. What do you check first?"
Answer:
1. Check the Execution Plan to locate bottlenecks (e.g., table scans vs. index seeks).

2. Verify if appropriate Indexes exist on columns used in WHERE, JOIN, and ORDER BY clauses.

3. Check for SARGability (Ensure functions or calculations aren't wrapped around indexed columns, which forces full table scans).

4. Consider table Partitioning if filtering by date ranges or regional keys.
Sql Interview question:

3. What is a Common Table Expression (CTE), and when is it better than a Subquery or Temporary Table?
Answer:
A CTE (WITH clause) provides a temporary result set that exists only during the execution of that specific query. It dramatically improves query readability over nested subqueries.

Temporary tables (#temp) are preferred instead if you need to reuse the dataset multiple times across a massive batch script or index the temporary data for performance.
👌1