SQL (Structured Query Language) is a standardized programming language used for managing and manipulating relational databases. It allows users to query, insert, update, and delete data stored in a relational database system (RDBMS). SQL provides a powerful interface to interact with data, making it the core tool for database administration, querying, and data analysis.
Here’s a detailed explanation of SQL, its components, and key concepts:
SQL commands are categorized into several types based on their functionality:
SELECT column1, column2, ... FROM table_name WHERE condition;
SELECT Name, Age FROM Students WHERE Major = 'Computer Science';
This query will retrieve the Name and Age of students who are majoring in Computer Science.CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Major VARCHAR(50)
);
This command creates a new table called Students with StudentID, Name, Age, and Major as columns.INSERT INTO Students (StudentID, Name, Age, Major)
VALUES (1, 'Alice', 20, 'Computer Science');
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
DELETE FROM Students WHERE StudentID = 1;
GRANT SELECT, INSERT ON Students TO user1;
REVOKE INSERT ON Students FROM user1;
BEGIN TRANSACTION;
UPDATE Students SET Age = 21 WHERE StudentID = 2;
COMMIT;
SQL includes several clauses and operators used to filter, group, and sort data:
SELECT * FROM Students WHERE Age > 18;
SELECT * FROM Students ORDER BY Name ASC;
COUNT, SUM).SELECT Major, COUNT(*) AS NumberOfStudents FROM Students GROUP BY Major;
SELECT Major, COUNT(*) AS NumberOfStudents
FROM Students
GROUP BY Major
HAVING COUNT(*) > 5;
SELECT * FROM Students WHERE Name LIKE 'A%';
This will return all students whose names start with the letter 'A'.WHERE clause.SELECT * FROM Students WHERE Major IN ('Computer Science', 'Physics');
SELECT * FROM Students WHERE Age BETWEEN 18 AND 22;
SELECT * FROM Students WHERE Major IS NULL;
SQL includes several aggregate functions to perform calculations on sets of data:
SELECT COUNT(*) FROM Students;SELECT SUM(Age) FROM Students;SELECT AVG(Age) FROM Students;SELECT MIN(Age) FROM Students;SELECT MAX(Age) FROM Students;Joins are used to combine rows from two or more tables based on a related column.
SELECT Students.Name, Courses.CourseName
FROM Students
INNER JOIN Enrollment ON Students.StudentID = Enrollment.StudentID
INNER JOIN Courses ON Enrollment.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName
FROM Students
LEFT JOIN Enrollment ON Students.StudentID = Enrollment.StudentID
LEFT JOIN Courses ON Enrollment.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName
FROM Students
RIGHT JOIN Enrollment ON Students.StudentID = Enrollment.StudentID
RIGHT JOIN Courses ON Enrollment.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName
FROM Students
FULL JOIN Enrollment ON Students.StudentID = Enrollment.StudentID
FULL JOIN Courses ON Enrollment.CourseID = Courses.CourseID;
A subquery is a query nested inside another query. Subqueries can be used in SELECT, INSERT, UPDATE, and DELETE statements.
SELECT Name, Age
FROM Students
WHERE StudentID IN (SELECT StudentID FROM Enrollment WHERE CourseID = 101);
SQL is an essential language for managing and manipulating data in relational databases. Its powerful commands allow for a wide range of operations, from simple queries to complex data manipulations and transactions. Mastering SQL is crucial for database administration, data analysis, and software development, as it provides the ability to efficiently query, manage, and update data in relational systems.
Open this section to load past papers