Welcome to the SQL Cheatsheet repository! This is a practical guide to SQL, covering data definition, queries, manipulation, control, and transactions. Whether you are a beginner or an experienced developer, this cheatsheet aims to simplify your SQL learning journey.
SQL (Structured Query Language) is the standard language for managing and manipulating databases. This repository provides a concise overview of essential SQL commands and concepts. You can find the latest updates and releases here.
To get started with this cheatsheet, download the latest release from the link above. The release contains all necessary files to help you understand and practice SQL commands effectively.
DDL is used to define and manage all database objects. Here are some common DDL commands:
- CREATE: Used to create a new table or database.
- ALTER: Used to modify an existing database object.
- DROP: Used to delete a table or database.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(50)
);
DML is used for manipulating data within tables. Common DML commands include:
- INSERT: Adds new records to a table.
- UPDATE: Modifies existing records.
- DELETE: Removes records from a table.
Example:
INSERT INTO employees (id, name, position) VALUES (1, 'Alice', 'Manager');
DQL is primarily concerned with querying data. The main command is:
- SELECT: Retrieves data from one or more tables.
Example:
SELECT * FROM employees WHERE position = 'Manager';
DCL is used to control access to data in the database. Common DCL commands include:
- GRANT: Gives users access privileges to database objects.
- REVOKE: Removes access privileges.
Example:
GRANT SELECT ON employees TO user1;
TCL manages transactions in the database. Key commands include:
- COMMIT: Saves all changes made during the current transaction.
- ROLLBACK: Undoes changes made during the current transaction.
Example:
BEGIN;
UPDATE employees SET position = 'Senior Manager' WHERE id = 1;
COMMIT;
Joins allow you to combine rows from two or more tables based on related columns. Common types of joins include:
- INNER JOIN: Returns records with matching values in both tables.
- LEFT JOIN: Returns all records from the left table and matched records from the right table.
- RIGHT JOIN: Returns all records from the right table and matched records from the left table.
- FULL OUTER JOIN: Returns records when there is a match in either left or right table records.
Example:
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Indexes improve the speed of data retrieval operations on a database table. They can be created on one or more columns.
Example:
CREATE INDEX idx_employee_name ON employees (name);
A view is a virtual table based on the result set of a SQL statement. It can simplify complex queries.
Example:
CREATE VIEW manager_view AS
SELECT * FROM employees WHERE position = 'Manager';
Stored procedures are a set of SQL statements that can be stored and executed on the database server.
Example:
CREATE PROCEDURE GetEmployeeById (IN emp_id INT)
BEGIN
SELECT * FROM employees WHERE id = emp_id;
END;
Here are some practical examples to illustrate SQL commands:
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10, 2)
);
INSERT INTO products (product_id, product_name, price)
VALUES (1, 'Laptop', 999.99);
UPDATE products
SET price = 899.99
WHERE product_id = 1;
DELETE FROM products
WHERE product_id = 1;
Contributions are welcome! If you have suggestions or improvements, please create a pull request. You can also report issues or request new features.
This project is licensed under the MIT License. See the LICENSE file for details.
For more resources and updates, visit our Releases section. Happy coding!