filmov
tv
🔥 5 Advanced SQL Interview Questions on Dynamic SQL, JSON & More! #SQL #AdvancedSQL #InterviewPrep

Показать описание
🚀 Elevate Your SQL Expertise with These Advanced Interview Questions!
✅ 1. What is Dynamic SQL and How Do You Execute It Safely?
Dynamic SQL allows you to construct and execute SQL statements at runtime. This is useful for building flexible queries but can expose your application to SQL injection if not handled properly.
Example in SQL Server:
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'SELECT * FROM Employees WHERE department = @dept';
Using parameterized queries helps prevent SQL injection.
✅ 2. How Do You Query and Manipulate JSON Data in SQL?
Modern SQL databases like SQL Server and PostgreSQL offer built-in JSON functions. These functions allow you to parse, query, and manipulate JSON data stored in a column.
Example in SQL Server:
-- Assume Employees table has a column 'details' storing JSON data.
SELECT name,
JSON_VALUE(details, '$.phone') AS phone,
JSON_QUERY(details, '$.address') AS address
FROM Employees;
This retrieves specific JSON attributes from the column.
✅ 3. What is a Deadlock in SQL and How Can It Be Resolved?
A deadlock occurs when two or more transactions block each other by holding locks that the other needs. Resolving deadlocks involves:
Minimizing transaction time.
Accessing resources in a consistent order.
Using appropriate isolation levels. Conceptual Example:
If Transaction A locks Table X and needs Table Y, while Transaction B locks Table Y and needs Table X, a deadlock occurs. Tools like SQL Server's deadlock graph help identify and troubleshoot such issues.
✅ 4. How Do You Implement Row-Level Security in SQL Databases?
Row-level security restricts data access at the row level based on user characteristics.
Example in SQL Server:
-- Create a predicate function that returns true for allowed rows.
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS fn_securitypredicate_result
WHERE @dept_id = SESSION_CONTEXT(N'dept_id');
-- Apply the security policy on the Employees table.
CREATE SECURITY POLICY EmployeeFilter
ON dbo.Employees;
This ensures users only see rows where the department matches their context.
✅ 5. What is Database Normalization and What Are the Different Normal Forms?
Database normalization organizes data to reduce redundancy and improve integrity. The most common normal forms include:
1NF (First Normal Form): Ensures atomicity of data (each column contains indivisible values).
2NF (Second Normal Form): Removes partial dependencies on a composite key.
3NF (Third Normal Form): Eliminates transitive dependencies.
BCNF (Boyce-Codd Normal Form): A stricter version of 3NF that handles certain anomalies. Understanding these forms helps in designing efficient and scalable databases.
💡 Master these advanced SQL topics to stand out in your next interview!
💬 Have questions or need further clarifications? Drop your queries in the comments!
#SQLInterview #AdvancedSQL #TechInterview #SQLTips #DataScience
✅ 1. What is Dynamic SQL and How Do You Execute It Safely?
Dynamic SQL allows you to construct and execute SQL statements at runtime. This is useful for building flexible queries but can expose your application to SQL injection if not handled properly.
Example in SQL Server:
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'SELECT * FROM Employees WHERE department = @dept';
Using parameterized queries helps prevent SQL injection.
✅ 2. How Do You Query and Manipulate JSON Data in SQL?
Modern SQL databases like SQL Server and PostgreSQL offer built-in JSON functions. These functions allow you to parse, query, and manipulate JSON data stored in a column.
Example in SQL Server:
-- Assume Employees table has a column 'details' storing JSON data.
SELECT name,
JSON_VALUE(details, '$.phone') AS phone,
JSON_QUERY(details, '$.address') AS address
FROM Employees;
This retrieves specific JSON attributes from the column.
✅ 3. What is a Deadlock in SQL and How Can It Be Resolved?
A deadlock occurs when two or more transactions block each other by holding locks that the other needs. Resolving deadlocks involves:
Minimizing transaction time.
Accessing resources in a consistent order.
Using appropriate isolation levels. Conceptual Example:
If Transaction A locks Table X and needs Table Y, while Transaction B locks Table Y and needs Table X, a deadlock occurs. Tools like SQL Server's deadlock graph help identify and troubleshoot such issues.
✅ 4. How Do You Implement Row-Level Security in SQL Databases?
Row-level security restricts data access at the row level based on user characteristics.
Example in SQL Server:
-- Create a predicate function that returns true for allowed rows.
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS fn_securitypredicate_result
WHERE @dept_id = SESSION_CONTEXT(N'dept_id');
-- Apply the security policy on the Employees table.
CREATE SECURITY POLICY EmployeeFilter
ON dbo.Employees;
This ensures users only see rows where the department matches their context.
✅ 5. What is Database Normalization and What Are the Different Normal Forms?
Database normalization organizes data to reduce redundancy and improve integrity. The most common normal forms include:
1NF (First Normal Form): Ensures atomicity of data (each column contains indivisible values).
2NF (Second Normal Form): Removes partial dependencies on a composite key.
3NF (Third Normal Form): Eliminates transitive dependencies.
BCNF (Boyce-Codd Normal Form): A stricter version of 3NF that handles certain anomalies. Understanding these forms helps in designing efficient and scalable databases.
💡 Master these advanced SQL topics to stand out in your next interview!
💬 Have questions or need further clarifications? Drop your queries in the comments!
#SQLInterview #AdvancedSQL #TechInterview #SQLTips #DataScience