Querying
✅ What is an SQL Query?
SQL (Structured Query Language) is a standard programming language used to communicate with relational databases. SQL Queries are commands used to retrieve, insert, update, delete, and manipulate data stored in relational databases such as MySQL, PostgreSQL, SQL Server, Oracle, SQLite, etc.
🎯 Types of SQL Queries
Data Query Language (DQL)
Retrieve data from databases
SELECT
Data Manipulation Language (DML)
Manage data (Insert, Update, Delete)
INSERT
, UPDATE
, DELETE
Data Definition Language (DDL)
Define and modify database structure
CREATE
, ALTER
, DROP
Data Control Language (DCL)
Control access to data
GRANT
, REVOKE
Transaction Control (TCL)
Manage transactions
COMMIT
, ROLLBACK
📊 Basic SQL Query Examples
1. SELECT (Retrieve Data)
sqlCopyEditSELECT first_name, last_name FROM employees;
Retrieves
first_name
andlast_name
fromemployees
table.
2. WHERE Clause (Filter Data)
sqlCopyEditSELECT * FROM employees WHERE department = 'Sales';
Retrieves all employees working in the Sales department.
3. INSERT (Add Data)
sqlCopyEditINSERT INTO employees (first_name, last_name, department)
VALUES ('John', 'Doe', 'Marketing');
Inserts a new employee record.
4. UPDATE (Modify Data)
sqlCopyEditUPDATE employees
SET department = 'Sales'
WHERE employee_id = 101;
Updates the department of employee with ID
101
.
5. DELETE (Remove Data)
sqlCopyEditDELETE FROM employees
WHERE employee_id = 101;
Deletes employee record with ID
101
.
🔑 Advanced SQL Query Examples
6. JOIN (Combine Tables)
sqlCopyEditSELECT employees.first_name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.id;
Retrieves employee names with their department names using a JOIN.
7. GROUP BY (Aggregate Data)
sqlCopyEditSELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
Shows the number of employees in each department.
8. ORDER BY (Sort Results)
sqlCopyEditSELECT * FROM employees
ORDER BY last_name ASC;
Lists employees sorted by last name alphabetically.
9. LIMIT (Restrict Rows Returned)
sqlCopyEditSELECT * FROM employees
LIMIT 5;
Shows only the first 5 employees.
10. LIKE (Pattern Matching)
sqlCopyEditSELECT * FROM employees
WHERE first_name LIKE 'J%';
Finds employees whose first names start with 'J'.
⚙️ Database Examples to Practice SQL Queries:
employees
Employee records (HR data)
customers
Customer data for sales or support systems
orders
E-commerce orders and transactions
products
Product catalog
departments
Organizational units
🌐 Popular SQL Databases:
MySQL
Open-source relational DB
Widely used, part of LAMP stack
PostgreSQL
Advanced open-source relational
ACID-compliant, powerful extensions
SQL Server
Microsoft relational DB
Enterprise-level, .NET friendly
SQLite
Embedded relational DB
Lightweight, great for apps/devices
Oracle Database
Commercial relational DB
High performance, enterprise-grade
🚀 Simple Use Case Scenario:
💡 Example: Get names of employees from "Sales" department sorted by last name.
sqlCopyEditSELECT first_name, last_name
FROM employees
WHERE department = 'Sales'
ORDER BY last_name;
✅ Summary Table:
Retrieve all data
SELECT * FROM table_name;
Retrieve specific columns
SELECT col1, col2 FROM table_name;
Filter records
SELECT * FROM table WHERE condition;
Insert new record
INSERT INTO table (col1, col2) VALUES (val1, val2);
Update existing record
UPDATE table SET col1 = val1 WHERE condition;
Delete record
DELETE FROM table WHERE condition;
Sort results
`ORDER BY column ASC
Aggregate data
GROUP BY column;
Join tables
JOIN
, LEFT JOIN
, RIGHT JOIN
Last updated