
SQL (Structured Query Language) continues to be an essential skill for data analysts, data scientists, backend developers, and database administrators. Interviewers often assess a candidate’s ability to query, manipulate, and understand data stored in relational databases. Below are ten fundamental SQL interview questions every job seeker should be prepared to solve. Each section includes a discussion of the concept behind the question and how to approach solving it.
1. Finding the Second Highest Salary
A classic question that tests both your understanding of subqueries and ordering data is: “How do you find the second highest salary from a table named Employees with a column Salary?” This question challenges the candidate to think beyond the basic MAX() function. The most common approach involves using a subquery to exclude the highest salary. For instance, you might write:

This SQL statement works by first retrieving the highest salary using the inner query and then selecting the next maximum value that is less than this result. Alternatively, one can use the DENSE_RANK() or ROW_NUMBER() window function to assign a rank to each salary and filter for the second position, which is often the preferred method in real-world scenarios due to better flexibility and performance on large datasets.
2. Retrieving Duplicate Records
Interviewers often want to assess your ability to detect and handle duplicates in a dataset. A common formulation is: “Find all duplicate email addresses in a Users table.” Solving this requires knowledge of grouping and filtering. The typical solution groups by the email field and uses the HAVING clause to count occurrences greater than one:

This query groups all the rows by email and then filters out groups that appear only once, revealing only those with duplicates. Understanding how to use GROUP BY in conjunction with HAVING is crucial for this type of question, and being able to extend this to return the full duplicate rows can show deeper SQL proficiency.
3. Joining Tables to Combine Information
An essential part of SQL interviews involves joining multiple tables. One typical question might be: “List all employees and their department names from Employees and Departments tables.” This tests your understanding of foreign keys and join operations. Assuming Employees has a DepartmentID field that relates to Departments.ID, the query would be:

This inner join ensures that only employees with a valid department ID in the Departments table are returned. Being comfortable with inner joins, left joins, and understanding when to use each is vital, as real-world databases are often normalized across many tables.
4. Aggregating Data with GROUP BY
A frequently asked question focuses on aggregation, such as: “Find the number of employees in each department.” This requires using GROUP BY along with aggregate functions like COUNT(). The solution would look like this:

This query groups the employees by their department and counts how many belong to each. Candidates should also be prepared to join this with the Departments table if the interviewer asks for department names instead of IDs. Mastery of aggregate functions is a critical skill for reporting and dashboard development.
5. Filtering with WHERE and HAVING
Sometimes interviewers combine conditions in the WHERE and HAVING clauses to see if you can distinguish their roles. For example: “List departments having more than 10 employees and located in ‘New York.’” Here, WHERE is used for row-level filtering, and HAVING for group-level. The query would be:

This structure filters rows before aggregation and then filters groups after aggregation. Misplacing conditions (like using HAVING where WHERE should be) is a common pitfall interviewers watch for.
6. Using CASE Statements for Conditional Logic
Another insightful question is: “Write a query that classifies employees as ‘Senior’ if their salary is above 100,000, and ‘Junior’ otherwise.” This tests the use of CASE for deriving new columns based on logic. The solution might look like this:

The CASE expression allows for readable conditional logic within SELECT statements. It’s commonly used in dashboards, reports, and when transforming raw data for business use.
7. Ranking Data with Window Functions
Advanced interviews often include questions about window functions. A common one is: “Rank employees by salary within each department.” This requires partitioning and ordering data within groups. The SQL might look like:

Window functions like RANK(), DENSE_RANK(), and ROW_NUMBER() are powerful tools for ranking and running totals. Demonstrating knowledge of PARTITION BY and ORDER BY clauses within OVER() shows a deeper understanding of SQL.
8. Finding Records Without Matches
A common real-world scenario is identifying rows that don’t have a corresponding entry in another table. A typical question might be: “Find all customers who have not placed any orders.” This requires a LEFT JOIN with a NULL check:

This query joins the two tables and filters to find customers with no related order. It tests your understanding of outer joins and NULL handling, a frequent need in reporting and data quality checks.
9. Working with Dates and Time Ranges
Handling date-based queries is another key interview area. One question could be: “Find all orders placed in the last 30 days.” This requires using date functions like CURRENT_DATE (or GETDATE() in some dialects):

Interviewers might follow up by asking for orders grouped by week or month, testing your knowledge of date formatting, truncation, and aggregation. Comfort with time functions is essential for real-world reporting.
10. Deleting or Updating Based on a Subquery
Finally, you might be asked to perform a DELETE or UPDATE using a condition derived from a subquery. For example: “Delete all products that were never ordered.” This combines filtering with referential logic:

Alternatively, a more performant version might use NOT EXISTS:

This type of question ensures you understand how to manipulate data safely using subqueries and conditions.
Conclusion
Mastering these ten SQL questions is more than just interview prep—it builds a foundation for solving real-world data challenges. Whether filtering data with precision, writing complex joins, or leveraging window functions for advanced analytics, these exercises develop fluency in SQL’s powerful capabilities. To further improve, practice variations of these questions, explore optimization techniques, and always be prepared to explain the logic behind your approach during interviews.
SQL عشر أسئلة قد تُحدد نجاحك أو فشلك في مقابلة

SQL لا تزال لغة الاستعلامات الهيكلية
مهارة أساسية لمحللي البيانات وعلماء البيانات ومطوري البرامج الخلفية ومديري قواعد البيانات، فغالباً ما يُقيّم القائمون على المقابلات قدرة المرشح على الاستعلام عن البيانات المخزنة في قواعد البيانات العلائقية ومعالجتها وفهمها، وفيما يلي عشرة أسئلة أساسية
SQL في مقابلات العمل بلغة
ينبغي على كل باحث عن عمل الاستعداد للإجابة عليها، بحيث يتضمن كل قسم مناقشة للمفهوم الكامن وراء السؤال وكيفية التعامل معه
1. إيجاد ثاني أعلى راتب
من الأسئلة الكلاسيكية التي تختبر فهمك للاستعلامات الفرعية وترتيب البيانات: كيف تجد ثاني أعلى راتب من جدول الموظفون مع عمود الراتب؟
الأساسية MAX() يتحدى هذا السؤال المرشح للتفكير فيما يتجاوز دالة
:بحيث تتضمن الطريقة الأكثر شيوعاً استخدام استعلام فرعي لاستبعاد أعلى راتب، فعلى سبيل المثال، يمكنك كتابة

SQL تعمل عبارة
هذه عن طريق استرجاع أعلى راتب باستخدام الاستعلام الداخلي ثم اختيار القيمة القصوى التالية الأقل من هذه النتيجة
ROW_NUMBER() أو DENSE_RANK() كبديل يُمكن استخدام دالة النافذة
لتعيين رتبة لكل راتب وتصفية المرتبة الثانية وهي غالباً الطريقة المُفضّلة في الحالات العملية نظراً لمرونتها وأدائها الأفضل على مجموعات البيانات الكبيرة
٢. استرداد السجلات المكررة
غالباً ما يرغب القائمون على المقابلات في تقييم قدرتك على اكتشاف السجلات المكررة ومعالجتها في مجموعة بيانات، فإحدى الصيغ الشائعة هي: البحث عن جميع عناوين البريد الإلكتروني المكررة في جدول المستخدمين يتطلب حل هذه المشكلة معرفةً بالتجميع والتصفية، بحيث يقوم الحل النموذجي بالتجميع حسب حقل البريد الإلكتروني
لحساب عدد مرات التكرار التي تزيد عن واحد HAVING ويستخدم شرط

يقوم هذا الاستعلام بتجميع جميع الصفوف حسب البريد الإلكتروني ثم يُرشِّح المجموعات التي تظهر مرة واحدة فقط ويكشف فقط عن المجموعات المكررة
HAVING مع GROUP BY يُعدّ فهم كيفية استخدام
أمراً بالغ الأهمية لهذا النوع من الأسئلة والقدرة على توسيع نطاقه لعرض جميع الصفوف المكررة
SQL يُظهر إتقاناً أعمق للغة
٣. ربط الجداول لدمج المعلومات
SQL يُعد ربط عدة جداول جزءاً أساسياً من مقابلات
قد يكون أحد الأسئلة الشائعة: اذكر جميع الموظفين وأسماء أقسامهم من جدولي الموظفين والأقسام، إذ يختبر هذا فهمك للمفاتيح الخارجية وعمليات الربط، وبافتراض أن حقل الموظفين
Departments.ID يحتوي على حقل معرف القسم المرتبط بـ
:سيكون الاستعلام كما يلي

يضمن هذا الربط الداخلي عرض الموظفين الذين لديهم معرف قسم صالح
Departments فقط في جدول
بحيث يُعدّ إتقان الربط الداخلي والربط الأيسر وفهم وقت استخدام كل منهما أمراً بالغ الأهمية، فغالباً ما يتم توحيد قواعد البيانات الفعلية عبر العديد من الجداول
4. GROUP BY تجميع البيانات باستخدام
يُركز سؤال شائع على التجميع مثل: ابحث عن عدد الموظفين في كل قسم
GROUP BY ويتطلب هذا استخدام
:سيكون الحل كالتالي COUNT() مع دوال التجميع مثل

يُجمّع هذا الاستعلام الموظفين حسب قسمهم ويحسب عدد الموظفين في كل قسم، ويجب على المرشحين أيضاً الاستعداد لربط هذا الاستعلام بجدول الأقسام إذا طلب المُقابل أسماء الأقسام بدلاً من مُعرّفاتها، ويُعدّ إتقان دوال التجميع مهارةً أساسيةً لإعداد التقارير وتطوير لوحات المعلومات
5. HAVINGو WHERE التصفية باستخدام
HAVINGو WHERE أحياناً يجمع المُقابلون الشروط في جملتي
لمعرفة ما إذا كان يُمكن تمييز أدوارهم، فعلى سبيل المثال: اذكر الأقسام التي تضم أكثر من ١٠ موظفين وتقع في نيويورك
HAVING للتصفية على مستوى الصف و WHERE وهنا يُستخدم
للتصفية على مستوى المجموعة، لذا سيكون الاستعلام كالتالي

يُصفّي هذا الهيكل الصفوف قبل التجميع ثم يُصفّي المجموعات بعد التجميع، ويُعدّ وضع الشروط في غير موضعها
WHERE حيث يجب أن يكون HAVING مثل استخدام
خطأً شائعاً يحذر منه المُقابلون
6. للمنطق الشرطي CASE استخدام عبارات
سؤالٌ آخر مُفيد: اكتب استعلاماً يُصنّف الموظفين إلى كبار إذا كان راتبهم أعلى من 100,000 ومبتدئ في غير ذلك
لاشتقاق أعمدة جديدة بناءً على المنطق CASE فيختبر هذا استخدام
:قد يبدو الحل كالتالي

SELECT بمنطق شرطي سهل القراءة ضمن عبارات CASE يسمح تعبير
ويُستخدم عادةً في لوحات المعلومات والتقارير وعند تحويل البيانات الخام للاستخدام التجاري
7. ترتيب البيانات باستخدام دوال النافذة
غالباً ما تتضمن المقابلات المتقدمة أسئلة حول دوال النافذة، ومن الأسئلة الشائعة: رتب الموظفين حسب الراتب في كل قسم، يتطلب هذا تقسيم البيانات وترتيبها داخل مجموعات
:كما يلي SQL وعليه قد يبدو

:تُعد دوال النافذة مثل
RANK(), DENSE_RANK(), ROW_NUMBER()
أدوات فعّالة لترتيب الإجماليات وإجرائها
ORDER BYو PARTITION BY ويُظهر إثبات معرفة جملتي
SQL فهماً أعمق لـ OVER() ضمن
8. البحث عن سجلات بدون تطابقات
من السيناريوهات الشائعة في الحياة العملية تحديد الصفوف التي لا تحتوي على مُدخل مُقابل في جدول آخر، قد يكون السؤال النموذجي: البحث عن جميع العملاء الذين لم يُقدموا أي طلبات
:NULL مع فحص LEFT JOIN يتطلب هذا استخدام

يربط هذا الاستعلام الجدولين ويُرشّح للعثور على العملاء الذين ليس لديهم طلب مُرتبط
NULL يختبر هذا فهمك للروابط الخارجية ومعالجة
وهي حاجة شائعة في إعداد التقارير وفحص جودة البيانات
9. العمل مع التواريخ والنطاقات الزمنية
تُعد معالجة الاستعلامات القائمة على التاريخ مجالاً رئيسياً آخر للمقابلات، فقد يكون أحد الأسئلة: البحث عن جميع الطلبات المُقدمة خلال آخر 30 يوماً، ويتطلب هذا استخدام دوال التاريخ مثل
في بعض اللهجات GETDATE() أو CURRENT_DATE

قد يطلب القائمون على المقابلات طلبات مُجمّعة حسب الأسبوع أو الشهر، ولاختبار معرفتك بتنسيق التاريخ والاقتطاع والتجميع، وعليه يُعدّ الإتقان في استخدام دوال الوقت أمراً أساسياً لإعداد التقارير العملية
10. الحذف أو التحديث بناءً على استعلام فرعي
أخيراً قد يُطلب منك إجراء حذف أو تحديث باستخدام شرط مُشتق من استعلام فرعي، فعلى سبيل المثال: حذف جميع المنتجات التي لم تُطلب أبداً، يجمع هذا بين التصفية والمنطق المرجعي

كبديل قد تستخدم نسخة أكثر فعالية : غير موجود

الخلاصة
العشر هذه SQL إن إتقان أسئلة
لا يقتصر على مجرد التحضير للمقابلة، بل يُرسي أساساً لحل تحديات البيانات العملية، فسواءً كنتَ تُرشِّح البيانات بدقة أو تكتب عمليات ربط معقدة أو تستخدم وظائف النوافذ للتحليلات المتقدمة فإن هذه التمارين تُنمّي إتقانك لقدرات هذه اللغة القوية، ولمزيد من التحسين تدرب على أشكال مختلفة من هذه الأسئلة واستكشف تقنيات التحسين وكن مستعداً دائماً لشرح منطق منهجك أثناء المقابلات

You must be logged in to post a comment.