أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
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);
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.
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 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. |
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
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
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.