SQL Queries
SQL Queries
SQL (Structured Query Language) is a powerful tool for managing and manipulating databases. It allows users to perform various operations such as retrieving, updating, deleting, and inserting data in a structured format.
1. SELECT Statement
The SELECT command retrieves specific records from a database table based on given conditions.
Syntax:
SELECT column1, column2, ... FROM table_name WHERE condition; ;
Retrieve all employees whose salary exceeds 50,000.
Example:
SELECT name, salary FROM employees WHERE salary > 50000;
2. INSERT INTO Statement
This command adds new data into a table.
Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Insert a new student into the students table.
Example:
INSERT INTO students (id, name, age, course) VALUES (101, 'John Doe', 22, 'Computer Science');
3. UPDATE Statement
Used to modify existing records in a table.
Syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Update the salary of an employee named "Alice".
Example:
UPDATE employees SET salary = 75000 WHERE name = 'Alice';
4. DELETE Statement
Removes records from a table based on specified conditions.
Syntax:
DELETE FROM table_name WHERE condition;
Delete records of students who failed the exam.
Example:
DELETE FROM students WHERE marks < 40;
5. CREATE TABLE Statement
Defines a new table structure.
Syntax:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
Create a customers table.
Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(255) UNIQUE,
age INT
);
6. ALTER TABLE Statement
Modifies an existing table’s structure.
Syntax (Adding a Column):
ALTER TABLE table_name ADD column_name datatype;
Add a phone_number column to the customers table.
Example:
ALTER TABLE customers ADD phone_number VARCHAR(15);
7. DROP TABLE Statement
Deletes an entire table along with its data.
Syntax:
DROP TABLE table_name;
Remove the backup_data table permanently.
Example:
DROP TABLE backup_data;
8. TRUNCATE TABLE Statement
Removes all records from a table but keeps its structure.
Syntax:
TRUNCATE TABLE table_name;
Delete all rows from the orders table but retain its schema.
Example:
TRUNCATE TABLE orders;
9. JOIN Clause
Used to combine rows from multiple tables based on a related column.
Syntax (INNER JOIN):
SELECT table1.column1, table2.column2 FROM table1 INNER JOIN table2 ON table1.common_column = table2.common_column;
Retrieve employee names along with their department names.
Example:
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
10. GROUP BY Clause
Groups rows that share a common value and applies aggregate functions.
Syntax:
SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name;
Find the total salary paid to each department.
Example:
SELECT department_id, SUM(salary) FROM employees GROUP BY department_id;
11. HAVING Clause
Filters grouped records based on aggregate function conditions.
Syntax:
SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name HAVING condition;
Find departments where total salary exceeds 100,000.
Example:
SELECT department_id, SUM(salary) FROM employees GROUP BY department_id HAVING SUM(salary) > 100000;
12. ORDER BY Clause
Sorts the result set in ascending (ASC) or descending (DESC) order.
Syntax:
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC;
List students in descending order of marks.
Example:
SELECT name, marks FROM students ORDER BY marks DESC;
13. DISTINCT Keyword
Eliminates duplicate values from a query result.
Syntax:
SELECT DISTINCT column_name FROM table_name;
Find unique job titles in a company.
Example:
SELECT DISTINCT job_title FROM employees;
14. LIMIT Clause
Restricts the number of records returned by a query.
Syntax:
SELECT column_name FROM table_name LIMIT number;
Retrieve the top 5 highest-paid employees.
Example:
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 5;
15. CASE Statement
Implements conditional logic in queries.
Syntax:
SELECT column_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END
FROM table_name;
Categorize employees based on salary.
Example:
SELECT name, salary,
CASE
WHEN salary > 80000 THEN 'High'
WHEN salary BETWEEN 50000 AND 80000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;
Prefer Learning by Watching?
Watch these YouTube tutorials to understand SQL Tutorial visually:
What You'll Learn:
- 📌 All SQL Fundamentals Explained in 10 Minutes | Learn SQL Basics for Beginners | SQL Crash Course
- 📌 Complete SQL Query in One Video | SQL Tutorial for Beginners| Complete MYSQL Query in One Video 2023