Basic SQL Queries
The following examples use a hypothetical table called Employees with columns like
EmployeeID, FirstName, LastName, Department, and Salary.
1. Retrieving Data (SELECT)
The SELECT statement is used to retrieve data from a database.
Query Purpose Example
SELECT * FROM TableName; Retrieves all columns and rows SELECT * FROM Employees;
from a specified table.
SELECT Column1, Column2 Retrieves specific columns from SELECT FirstName,
FROM TableName; a table. LastName, Department FROM
Employees;
2. Filtering Data (WHERE)
The WHERE clause is used to filter records and extract only those that fulfill a specified
condition.
Query Purpose Example
SELECT * FROM TableName Retrieves all columns where a SELECT * FROM Employees
WHERE Condition; specific condition is met. WHERE Department = 'Sales';
SELECT * FROM TableName Uses comparison operators SELECT * FROM Employees
WHERE Column > Value; (e.g., >, <, >=, <=, <>). WHERE Salary > 60000;
SELECT * FROM TableName Filters for records where a SELECT * FROM Employees
WHERE Column IN (Value1, column's value is in a list of WHERE Department IN ('IT',
Value2); specified values. 'HR');
SELECT * FROM TableName Filters for records where a SELECT * FROM Employees
WHERE Column LIKE column's value matches a WHERE LastName LIKE 'S%';
Pattern; specified pattern (use % as a (Finds last names starting with
wildcard). 'S')
SELECT * FROM TableName Combines conditions using SELECT * FROM Employees
WHERE Condition1 AND logical operators (AND, OR, WHERE Department = 'IT' AND
Condition2; NOT). Salary > 70000;
3. Sorting Data (ORDER BY)
The ORDER BY clause is used to sort the result set of a query.
Query Purpose Example
SELECT * FROM TableName Sorts the result set by the SELECT * FROM Employees
ORDER BY Column; specified column in ascending ORDER BY LastName;
order (default).
SELECT * FROM TableName Sorts the result set by the SELECT * FROM Employees
ORDER BY Column DESC; specified column in ORDER BY Salary DESC;
descending order.
4. Basic Data Manipulation (CRUD)
These queries are used to Create, Read (covered by SELECT), Update, and Delete data.
Statement Purpose Example
INSERT INTO TableName Inserts a new row into a table. INSERT INTO Employees
(Col1, Col2) VALUES (Val1, (FirstName, LastName,
Val2); Department) VALUES ('Jane',
'Doe', 'Marketing');
UPDATE TableName SET Modifies existing records in a UPDATE Employees SET
Col1 = NewVal WHERE table. Crucial: Always use Salary = 65000 WHERE
Condition; WHERE to avoid updating all EmployeeID = 101;
rows.
DELETE FROM TableName Deletes existing records from a DELETE FROM Employees
WHERE Condition; table. Crucial: Always use WHERE Department = 'HR';
WHERE to avoid deleting all
rows.
5. Aggregation and Grouping
These queries are used to perform calculations on a set of rows and return a single summary
value.
Query Purpose Example
SELECT COUNT(*) FROM Counts the number of rows in a SELECT COUNT(*) FROM
TableName; table. Employees;
SELECT AVG(Column) FROM Calculates the average value SELECT AVG(Salary) FROM
TableName; of a numeric column. Employees;
SELECT SUM(Column) FROM Calculates the sum of a SELECT SUM(Salary) FROM
TableName; numeric column. Employees;
SELECT Column, COUNT(*) Groups rows that have the SELECT Department,
FROM TableName GROUP BY same values and applies an COUNT(*) AS
Column; aggregate function to each NumberOfEmployees FROM
group. Employees GROUP BY
Department;