Beginner-Level SQL Questions
1. What is SQL?
SQL (Structured Query Language) is a programming language used to communicate with and manage databases. It allows performing operations like retrieving, inserting, updating, and deleting data.
2. What is a database?
A database is an organized collection of data stored and managed electronically. Examples include MySQL, PostgreSQL, and MongoDB.
3. What is a table in SQL?
A table is a collection of rows and columns that stores data in a structured format. Each column represents a field, and each row represents a record.
4. What is a primary key?
A primary key is a unique identifier for a row in a table. It ensures that no two rows have the same value in the primary key column.
Example:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);5. What is a foreign key?
A foreign key is a field in one table that references the primary key in another table, creating a relationship between the two.
Example:
CREATE TABLE Orders (
OrderID INT,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(ID)
);6. What is the difference between SQL and MySQL?
- SQL is the language used to interact with databases.
- MySQL is a database management system (DBMS) that uses SQL as its query language.
7. What is the difference between DELETE and TRUNCATE?
- DELETE: Removes specific rows based on a condition. It logs each row deletion and can be rolled back.
- TRUNCATE: Removes all rows from a table quickly without logging individual row deletions.
8. What are the different types of SQL commands?
SQL commands are categorized into:
- DDL (Data Definition Language):
CREATE,ALTER,DROP - DML (Data Manipulation Language):
SELECT,INSERT,UPDATE,DELETE - DCL (Data Control Language):
GRANT,REVOKE - TCL (Transaction Control Language):
COMMIT,ROLLBACK
9. What is a join in SQL?
A join is used to combine data from two or more tables based on a related column.
Example:
SELECT Employees.Name, Orders.OrderID
FROM Employees
JOIN Orders ON Employees.ID = Orders.EmployeeID;10. What are the different types of joins in SQL?
- INNER JOIN: Returns rows with matching values in both tables.
- LEFT JOIN: Returns all rows from the left table and matching rows from the right table.
- RIGHT JOIN: Returns all rows from the right table and matching rows from the left table.
- FULL JOIN: Returns rows when there is a match in either table.
11. What is a constraint in SQL?
A constraint enforces rules on a column to maintain data integrity. Examples include NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY.
12. What is the difference between WHERE and HAVING?
- WHERE: Filters rows before grouping.
- HAVING: Filters groups after aggregation.
Example:
SELECT Department, COUNT(*)
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 5;13. What is the GROUP BY clause?
GROUP BY is used to arrange identical data into groups. It’s often used with aggregate functions like COUNT, SUM, MAX, etc.
14. What is the difference between COUNT(*) and COUNT(column)?
COUNT(*): Counts all rows, including those withNULLvalues.COUNT(column): Counts rows with non-NULLvalues in the specified column.
15. What is an alias in SQL?
An alias gives a temporary name to a column or table in a query.
Example:
SELECT Name AS EmployeeName
FROM Employees;16. What is a self-join?
A self-join is a join where a table is joined with itself.
Example:
SELECT A.Name, B.Name AS Manager
FROM Employees A
JOIN Employees B ON A.ManagerID = B.ID;17. What is a case statement in SQL?
A CASE statement provides conditional logic in queries.
Example:
SELECT Name,
CASE
WHEN Age > 30 THEN 'Senior'
ELSE 'Junior'
END AS Category
FROM Employees;18. How do you fetch unique records in SQL?
Use the DISTINCT keyword.
Example:
SELECT DISTINCT Department
FROM Employees;19. How do you sort data in SQL?
Use the ORDER BY clause.
Example:
SELECT Name, Age
FROM Employees
ORDER BY Age DESC;20. What is the LIMIT clause in SQL?
LIMIT restricts the number of rows returned.
Example:
SELECT * FROM Employees
LIMIT 5;