SQL Interview Preparation Q&A | Part -1

Yogi Siddeswara 0

1. What is SQL, and why is it used?

SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It helps to query, insert, update, and delete data efficiently.

  SELECT * FROM Customers WHERE Country = 'India';
(code-box)

2. What are the different types of SQL statements?

SQL statements are categorized into five types:

  • DDL (Data Definition Language)
  • DML (Data Manipulation Language)
  • DQL (Data Query Language)
  • TCL (Transaction Control Language)
  • DCL (Data Control Language)
 CREATE TABLE Employees (
  ID INT PRIMARY KEY,
  Name VARCHAR(50),
  Salary DECIMAL(10, 2)
);(code-box)

3. What is the difference between DELETE and TRUNCATE?

DELETE: Removes specific rows based on a condition. It can be rolled back.

TRUNCATE: Removes all rows from a table. It is faster but cannot be rolled back.

  DELETE FROM Employees WHERE ID = 1;
TRUNCATE TABLE Employees;(code-box)

4. What is a JOIN in SQL?

JOINs are used to combine rows from two or more tables based on a related column. Common types are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.

 SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;(code-box)

5. What are indexes in SQL?

An index is used to speed up the retrieval of rows by creating a pointer to data in the database. It improves query performance.

 CREATE INDEX idx_customer_name ON Customers (CustomerName);(code-box)

6. What is normalization in SQL?

Normalization is the process of organizing data to reduce redundancy and improve data integrity. Common forms include 1NF, 2NF, 3NF, and BCNF.

7. What is a primary key?

A primary key is a unique identifier for a table row. It ensures that no two rows have the same value for the primary key column.

 CREATE TABLE Students (
  StudentID INT PRIMARY KEY,
  Name VARCHAR(50)
);(code-box)

8. What is a foreign key?

A foreign key is a field that establishes a relationship between two tables. It references the primary key in another table.

 CREATE TABLE Orders (
  OrderID INT PRIMARY KEY,
  CustomerID INT,
  FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);(code-box)

9. What is a stored procedure?

A stored procedure is a precompiled SQL code that can be executed repeatedly. It helps to encapsulate complex logic and improve performance.

 CREATE PROCEDURE GetCustomerOrders
AS
SELECT * FROM Orders WHERE CustomerID = 1;(code-box)

10. What is a transaction in SQL?

A transaction is a sequence of operations performed as a single logical unit of work. Transactions follow ACID properties (Atomicity, Consistency, Isolation, Durability).

 BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 500 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 500 WHERE AccountID = 2;
COMMIT;(code-box)

Post a Comment

0 Comments