SQL (Structured Query Language) is a standard language for storing, manipulating, and retrieving data in databases.
Common SQL data types include:
-- This is a single line comment
/*
This is a
multi-line comment
*/
DDL is used to define and modify the structure of database objects.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
hire_date DATE,
salary DECIMAL(10, 2)
);
-- Add a column
ALTER TABLE employees ADD email VARCHAR(100);
-- Modify a column
ALTER TABLE employees ALTER COLUMN email VARCHAR(150);
-- Drop a column
ALTER TABLE employees DROP COLUMN email;
DROP TABLE employees;
DML is used to manipulate data within the database.
INSERT INTO employees (employee_id, first_name, last_name, hire_date, salary)
VALUES (1, 'John', 'Doe', '2023-01-15', 50000.00);
UPDATE employees
SET salary = 55000.00
WHERE employee_id = 1;
DELETE FROM employees
WHERE employee_id = 1;
DQL is used to retrieve data from the database.
SELECT * FROM employees;
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000
ORDER BY last_name ASC;
SELECT * FROM employees
WHERE hire_date >= '2023-01-01' AND salary < 60000;
SELECT * FROM employees
WHERE last_name LIKE 'S%';
SELECT * FROM employees
WHERE department_id IN (1, 2, 3);
SELECT * FROM employees
WHERE salary BETWEEN 40000 AND 60000;
Joins are used to combine rows from two or more tables based on a related column between them.
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.department_id;
Aggregate functions perform calculations on a set of values and return a single result.
SELECT
COUNT(*) as total_employees,
AVG(salary) as average_salary,
MAX(salary) as highest_salary,
MIN(salary) as lowest_salary,
SUM(salary) as total_salary
FROM employees;
SELECT department_id, AVG(salary) as avg_salary
FROM employees
GROUP BY department_id;
SELECT department_id, AVG(salary) as avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 50000;
A subquery is a query nested inside another query.
SELECT first_name, last_name
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE department_name = 'IT'
);
SELECT
first_name,
last_name,
salary,
(SELECT AVG(salary) FROM employees) as avg_salary
FROM employees;
Indexes are used to speed up data retrieval operations.
CREATE INDEX idx_last_name ON employees (last_name);
DROP INDEX idx_last_name ON employees;
A view is a virtual table based on the result of a SELECT statement.
CREATE VIEW high_salary_employees AS
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 60000;
SELECT * FROM high_salary_employees;
Stored procedures are prepared SQL statements that can be saved and reused.
CREATE PROCEDURE GetEmployeesByDepartment
@DepartmentId INT
AS
BEGIN
SELECT * FROM employees
WHERE department_id = @DepartmentId;
END;
EXEC GetEmployeesByDepartment @DepartmentId = 1;
Triggers are special stored procedures that automatically run when certain events occur.
CREATE TRIGGER trg_UpdateModifiedDate
ON employees
AFTER UPDATE
AS
BEGIN
UPDATE employees
SET modified_date = GETDATE()
WHERE employee_id IN (SELECT employee_id FROM inserted);
END;
Transactions ensure that a set of SQL operations are performed as a single unit of work.
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
IF @@ERROR = 0
COMMIT TRANSACTION;
ELSE
ROLLBACK TRANSACTION;
DCL is used to control access to data within the database.
GRANT SELECT, INSERT ON employees TO user1;
REVOKE INSERT ON employees FROM user1;
EXPLAIN SELECT * FROM employees WHERE last_name = 'Smith';
SELECT *
when possible2024 © All rights reserved - buraxta.com