SQL Querying: A Beginner's Guide - Student Handout
SQL Querying: A Beginner's Guide - Student Handout
Handout
Welcome to the world of SQL (Structured Query Language)! This handout will guide you
through the basics and some advanced concepts of SQL, the language used to interact with
databases.
Syntax:
Examples:
b. INSERT Statement
The INSERT statement adds new data to a table.
Syntax:
Examples:
INSERT INTO students (name, age) VALUES ('Anjali', 21), ('Vikram', 23);
c. UPDATE Statement
The UPDATE statement modifies existing data in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Examples:
d. DELETE Statement
The DELETE statement removes data from a table.
Syntax:
Examples:
1. Remove a student:
2. Delete a course:
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Examples:
Syntax:
Examples:
b. LIMIT Clause
Limits the number of records returned by a query.
Syntax:
Examples:
1. First 5 students:
2. Top 3 courses:
4. SQL Joins
Joins combine data from multiple tables based on a related column.
a. INNER JOIN
Returns records with matching values in both tables.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Examples:
b. LEFT JOIN
Returns all records from the left table and matched records from the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Examples:
c. RIGHT JOIN
Returns all records from the right table and matched records from the left table.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Examples:
d. FULL JOIN
Returns all records when there is a match in either left or right table.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
Examples:
5. Advanced SQL
a. Aggregate Functions
Perform calculations on a set of values and return a single value.
Examples:
1. Count students:
Syntax:
Examples:
SELECT column1
FROM table_name
WHERE column2 = (SELECT column2 FROM another_table WHERE condition);
Examples:
1. Oldest student:
SELECT name
FROM students
WHERE age = (SELECT MAX(age) FROM students);
SELECT name
FROM students
WHERE course_id = (SELECT course_id FROM courses WHERE course_name =
'Mathematics');
SELECT name
FROM students
WHERE age > (SELECT AVG(age) FROM students);
Conclusion
This handout provides a comprehensive overview of SQL querying, covering basic commands
like SELECT , INSERT , UPDATE , and DELETE , as well as advanced topics like joins, aggregate
functions, and subqueries. Practice these queries to become proficient in SQL!