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)
Retrieves
first_name
andlast_name
fromemployees
table.
2. WHERE Clause (Filter Data)
Retrieves all employees working in the Sales department.
3. INSERT (Add Data)
Inserts a new employee record.
4. UPDATE (Modify Data)
Updates the department of employee with ID
101
.
5. DELETE (Remove Data)
Deletes employee record with ID
101
.
🔑 Advanced SQL Query Examples
6. JOIN (Combine Tables)
Retrieves employee names with their department names using a JOIN.
7. GROUP BY (Aggregate Data)
Shows the number of employees in each department.
8. ORDER BY (Sort Results)
Lists employees sorted by last name alphabetically.
9. LIMIT (Restrict Rows Returned)
Shows only the first 5 employees.
10. LIKE (Pattern Matching)
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.
✅ 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