أسئلة القسم

تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.

What are types of variables in SQL? سهل

Types of Variables in SQL:

  • Local variables: Declared using DECLARE inside a batch or stored procedure.
  • Global variables: Predefined system variables starting with @@, e.g., @@ROWCOUNT, @@VERSION.
What are types of Functions? سهل

Types of Functions in SQL:

  • Scalar Functions: Return a single value (e.g., LEN(), GETDATE()).
  • Aggregate Functions: Operate on a set of values and return one result (e.g., SUM(), AVG()).
  • Table-Valued Functions: Return a table (Inline or Multi-Statement).
What is constraint? سهل

A constraint is a rule applied to a column or table to enforce data integrity.

Types of Constraints:

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • CHECK
  • DEFAULT
  • NOT NULL
What is Composite Key and Unique Key? سهل
  • Composite Key: A key made of two or more columns to uniquely identify a row.
  • Unique Key: Ensures uniqueness for a column (or set of columns), allows one NULL value.
What is the difference between Primary Key and Foreign Key? سهل
Primary Key Foreign Key
Uniquely identifies a row. Refers to primary key in another table.
Only one allowed per table. Multiple FKs allowed.
No NULLs. Can allow NULLs.
What are Types of Joins? سهل

Types of Joins in SQL:

  • INNER JOIN: Returns matching rows.
  • LEFT JOIN: All rows from left table + matching from right.
  • RIGHT JOIN: All rows from right table + matching from left.
  • FULL OUTER JOIN: All rows, matched or unmatched.
  • CROSS JOIN: Cartesian product.
  • SELF JOIN: Join table with itself.
What is alias in SQL? سهل

An alias is a temporary name for a table or column.

Example:

SELECT e.Name AS EmployeeName FROM Employees e;
What is difference between Union and Union All? سهل
UNION UNION ALL
Removes duplicates. Keeps duplicates.
Slightly slower. Faster.
What is different between Where and Having? سهل
  • WHERE: Filters rows before grouping.
  • HAVING: Filters groups/aggregates after grouping.
What is different between delete, truncate and drop? متوسط
DELETE TRUNCATE DROP
Removes rows (can filter with WHERE). Removes all rows (faster). Removes table schema + data.
Logged, slower. Minimal logging. DDL operation, can't rollback easily.
Can rollback. Can rollback (if in transaction). Cannot rollback (drops table).
Write a SQL query to find the 2nd highest salary from an Employee table متوسط
SELECT MAX(Salary)
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
Write query for Nth Highest Salary in SQL صعب
SELECT DISTINCT Salary
FROM Employees e1
WHERE N-1 = (
   SELECT COUNT(DISTINCT Salary)
   FROM Employees e2
   WHERE e2.Salary > e1.Salary
);
Extended scenario: If multiple employees have same salary - write using Dense_Rank() صعب
SELECT *
FROM (
   SELECT Name, Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS rnk
   FROM Employees
) t
WHERE rnk = N;
What is the Dynamic Query in SQL? متوسط

A Dynamic Query is a query built as a string at runtime and executed with EXEC or sp_executesql.

Used when table/column names aren't known at compile time.

Explain CTE (Common Table Expression) with example متوسط

A CTE (Common Table Expression) is a temporary named result set defined with WITH.

Example:

WITH EmployeeCTE AS (
   SELECT Id, Name, ManagerId FROM Employees
)
SELECT * FROM EmployeeCTE WHERE ManagerId IS NULL;
What is difference between EXISTS and IN? متوسط
  • EXISTS: Checks existence (returns TRUE/FALSE).
  • IN: Matches a value against a list/subquery.
  • Note: EXISTS is usually faster for correlated subqueries.
Explain the difference between Subquery and Correlated Subquery صعب
  • Subquery: Runs once, result passed to outer query.
  • Correlated Subquery: Runs for each row of outer query.
What is Synonym in SQL? متوسط

A Synonym is an alias for a database object (table, view, proc).

Example:

CREATE SYNONYM Emp FOR HR.Employees;
What is different between View vs Stored Procedure? متوسط
View Stored Procedure
Virtual table (SELECT query). Precompiled set of SQL statements.
Cannot accept parameters (except indexed views). Accepts parameters.
Used for simplifying queries. Used for logic & transactions.
What is indexed view? صعب

An Indexed View is a view with a unique clustered index.

It materializes results, improving performance on complex queries.

What is index and types of indexes in SQL? متوسط

An Index is a structure to speed up data retrieval.

Types:

  • Clustered: One per table, sorts data.
  • Non-clustered: Like a separate lookup table.
  • Unique Index
  • Filtered Index
  • Full-Text Index
What is the difference between Clustered and Non-Clustered Index? متوسط
Clustered Non-Clustered
Sorts data physically. Stores pointers to data.
Only one allowed. Many allowed.
Faster for range queries. Faster for specific lookups.
How to implement Partitioning in SQL? صعب

Partitioning splits large tables into smaller, manageable parts.

Types:

  • Range
  • List
  • Hash
  • Composite
What are Triggers in SQL? Types of Triggers متوسط

Triggers are special procedures executed automatically on events (INSERT, UPDATE, DELETE).

Types:

  • DML
  • DDL
  • Instead-of
  • After
Explain ACID properties in databases with real examples متوسط

ACID Properties:

  • Atomicity: All or nothing (e.g., bank transfer completes or fails entirely).
  • Consistency: Valid state maintained (e.g., balance can't go negative).
  • Isolation: Transactions don't interfere (e.g., concurrent transfers don't mix).
  • Durability: Changes persist (e.g., committed transfer survives crash).
What is Normalization? Explain different Normal Forms (1NF, 2NF, 3NF, BCNF) متوسط

Normalization is the process of reducing redundancy.

Normal Forms:

  • 1NF: Atomic values.
  • 2NF: No partial dependency.
  • 3NF: No transitive dependency.
  • BCNF: Stronger 3NF.
What is Denormalization? When to use it? متوسط

Denormalization is adding redundancy back for performance.

Used in reporting, OLAP systems.

What are Transactions in SQL? How to use COMMIT and ROLLBACK? متوسط

A Transaction is a logical unit of work.

Example:

BEGIN TRAN;
UPDATE Accounts SET Balance = Balance - 100 WHERE Id = 1;
UPDATE Accounts SET Balance = Balance + 100 WHERE Id = 2;
COMMIT;
-- Or ROLLBACK if error occurs
What is a Deadlock in SQL? How can you prevent it? صعب

A Deadlock occurs when two transactions wait on each other's locks.

Prevention:

  • Keep transactions short.
  • Access objects in same order.
  • Use proper indexing.
Difference between OLTP and OLAP databases متوسط
OLTP OLAP
Transactional systems. Analytical systems.
Normalized schema. Denormalized/star schema.
Many small operations. Few complex queries.
What is SQL Profiling? متوسط

SQL Profiling is monitoring SQL queries to detect slow performance.

Tools:

  • SQL Server Profiler
  • Extended Events
  • Query Store
Write steps/queries for SQL Performance tuning صعب

Steps for SQL Performance Tuning:

  • Check execution plans.
  • Add proper indexes.
  • Avoid cursors, prefer set-based operations.
  • Use appropriate JOINs.
  • Optimize queries & schema.
What is GAC? (Global Assembly Cache, but related to data access) متوسط

The Global Assembly Cache (GAC) is a machine-wide cache for .NET assemblies (shared libraries).

Used when multiple apps share the same assembly version.

Explain database connectivity in .NET متوسط

Database connectivity in .NET is done via ADO.NET or Entity Framework Core.

ADO.NET Steps:

  • SqlConnection: Connect to database.
  • SqlCommand: Execute query.
  • SqlDataReader: Fetch results.
What types of caching are available in .NET? (Related to SQL data) صعب
  • In-Memory Cache: Using IMemoryCache.
  • Distributed Cache: Using IDistributedCache (e.g., Redis, SQL Server).
  • Output Caching: For web responses.
  • EF Core Caching: Query results caching via 3rd-party libraries.