Skip to main content

Top SQL Interview Question: How to Optimize Queries Based on Your Job Role

By April 4, 2025Blog

SQL Server Interview Guide: Query Optimization Based on Job Roles

One of the most common and practical SQL Server interview questions is: “How would you optimize a complex query?” Your answer should vary based on your role—Data Analyst, Data Engineer, SQL Developer, or Database Administrator—since each has different responsibilities and permissions.

In this blog, we break down a real-world sales report query and show how to optimize it according to each role. Whether you’re preparing for an interview or handling performance issues in your project, these steps will guide you.

 

The Scenario Query

You’re working with a Product database and asked to generate a report that shows year-wise, quarter-wise, month-wise, and product-wise sales totals, but only if sales are greater than 10,000.

Base Query:

SELECT T.CalendarYear, T.CalendarQuarter, T.EnglishMonthName, PRODUCTS_DATA.EnglishProductName,

SUM(S.SalesAmount) AS TOTAL_SALES

FROM SALES_DATA AS S

INNER JOIN TIME_DATA AS T ON S.OrderDateKey = T.TimeKey

INNER JOIN PRODUCTS_DATA ON PRODUCTS_DATA.ProductKey = S.ProductKey

WHERE T.CalendarYear = 2002

GROUP BY T.CalendarYear, T.CalendarQuarter, T.EnglishMonthName, PRODUCTS_DATA.EnglishProductName

HAVING SUM(S.SalesAmount) > 10000

ORDER BY TOTAL_SALES;

🔹 Optimization as a Data Analyst

Data Analysts typically have read-only access to production databases. Their options for optimization include:

✅ Step-by-step:

  1. Use Lock Hints to avoid blocking other users:

FROM SALES_DATA AS S WITH (READPAST)

  1. Use Join Hints to help the engine choose efficient join algorithms:

INNER MERGE JOIN TIME_DATA AS T WITH (READPAST)

  1. Execute the optimized query and validate result consistency.

These minor adjustments help prevent query delays without modifying database structure.

🔹 Optimization as a Data Engineer

Data Engineers have more permissions and can embed logic into programmable objects.

✅ Step-by-step:

  1. Apply analyst techniques (lock hints and join hints).
  2. Wrap the query inside a stored procedure:

CREATE PROCEDURE SP_REPORT_SALES AS

— Optimized query here

  1. Execute the stored procedure:

EXEC SP_REPORT_SALES;

Stored procedures are pre-compiled and reusable—ideal for automation or scheduled reporting.

🔹 Optimization as a Database Administrator (DBA)

DBAs have the highest level of access and focus on performance tuning.

✅ Step-by-step:

  1. Follow Data Engineer’s steps (hints + stored procedure).
  2. Run the Database Tuning Advisor (DTA) on the query to identify missing indexes or statistics.
  3. Create suggested indexes to speed up joins and filters.

Example:

CREATE INDEX IDX_Time_Year ON TIME_DATA(CalendarYear);

CREATE INDEX IDX_Sales_Amount ON SALES_DATA(SalesAmount);

  1. Monitor query plans and IO statistics.

🔹 Optimization as a SQL Developer

SQL Developers build logic in dev/staging environments and push it to production with proper review.

✅ Step-by-step:

  1. Use lock and join hints.
  2. Create stored procedure.
  3. Suggest and test index creation in sandbox environment:

CREATE INDEX IDX_Product_Key ON PRODUCTS_DATA(ProductKey);

  1. Refactor query logic if needed (e.g., reduce subqueries, use temp tables).

Bonus: Flexible Reporting with Table-Valued Functions (TVFs)

For dynamic reporting by year, use a parameterized function:

CREATE FUNCTION DN_REPORT_SALES (@year SMALLINT)

RETURNS TABLE AS

RETURN (

  SELECT T.CalendarYear, T.CalendarQuarter, T.EnglishMonthName, PRODUCTS_DATA.EnglishProductName,

  SUM(S.SalesAmount) AS TOTAL_SALES

  FROM SALES_DATA AS S WITH (READPAST)

  INNER JOIN TIME_DATA AS T WITH (READPAST) ON S.OrderDateKey = T.TimeKey

  INNER JOIN PRODUCTS_DATA WITH (READPAST) ON PRODUCTS_DATA.ProductKey = S.ProductKey

  WHERE T.CalendarYear = @year

  GROUP BY T.CalendarYear, T.CalendarQuarter, T.EnglishMonthName, PRODUCTS_DATA.EnglishProductName

  HAVING SUM(S.SalesAmount) > 10000

);

Execution:

SELECT * FROM DN_REPORT_SALES(2001);

SELECT * FROM DN_REPORT_SALES(2002);

SELECT * FROM DN_REPORT_SALES(2003) WHERE CalendarQuarter IN (1, 2);

Functions offer flexibility and dynamic filtering that are not available in views or static queries.

Each role plays a different part in SQL query optimization. Analysts avoid blocking, Engineers ensure reusability, DBAs tune for performance, and Developers structure efficient logic.





🎯 Want to master all these roles and answer confidently in interviews?

Join SQL School for expert-led training:

✅ Learn Query Optimization, Indexing, and Stored Procedures
✅ Practice with Real-time Projects and Hands-on Scenarios
✅ Prepare for Interviews with Mock Q&A and Live Labs

📞 Call +91 96666 40801 or visit 👉 SQL School to book your FREE demo session today!

👉 Fast-track your SQL career with practical training from industry experts. Join SQL School today!

SQL School – India’s #1 Trusted Training Institute for SQL Server, Azure, and Data Engineering.

Leave a Reply

×

Reach Us Now!

×