1)What is SQL?
ANS: SQL (Structured Query Language) is a standard programming language for managing and manipulating relational databases.
2)What are the different types of SQL commands?
ANS: SQL commands are categorized into:
DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE
DCL (Data Control Language): GRANT, REVOKE
TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
3)What is a primary key?
ANS: A primary key is a unique identifier for each record in a database table. It must contain unique v Values and cannot contain NULLs.
4)What is a foreign key?
ANS: A foreign key is a field (or collection of fields) in one table that uniquely identifies a row in another table, establishing a relationship between the two tables.
5)What is normalization?
ANS: Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller, related tables.
Intermediate SQL Questions
6)What are the different types of JOINs?
ANS: INNER JOIN: Returns records with matching values in both tables.
LEFT JOIN: Returns all records from the left table and matched records from the right table.
RIGHT JOIN: Returns all records from the right table and matched records from the left table.
FULL OUTER JOIN: Returns all records when there is a match in either left or right table records.
7)How do you find duplicate records in a table?
ANS: SELECT column_name, COUNT() FROM table_name GROUP BY column_name HAVING COUNT() > 1;
8)What is a view?
ANS: A view is a virtual table based on the result of a SELECT query. It can simplify complex queries and provide an additional layer of security.
9)What is the difference between HAVING and WHERE?
ANS: WHERE filters records before grouping, while HAVING filters records after grouping.
10)What is a stored procedure?
ANS: A stored procedure is a precompiled collection of SQL statements that can be executed as a single unit. It can accept parameters and is stored in the database for reuse.
Advanced SQL Questions
11)What is an index?
ANS: An index is a database object that improves the speed of data retrieval operations on a database table at the cost of additional space and slower write operations.
12)What is a trigger?
ANS: A trigger is a set of SQL statements that automatically execute in response to certain events on a particular table or view, such as INSERT, UPDATE, or DELETE.
13)How can you improve the performance of a SQL query?
ANS: Strategies include:
Creating and optimizing indexes.
Analyzing and rewriting queries for efficiency.
Avoiding SELECT * and selecting only necessary columns.
Using JOIN instead of subqueries when possible.