Skip to content

Resumen práctico de SQL: definición de datos, consultas, manipulación, control y transacciones.

Notifications You must be signed in to change notification settings

muhamadsuharto/sql-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

SQL Cheatsheet 📊

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.

Download Releases

Table of Contents

Introduction

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.

Getting Started

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.

SQL Basics

Data Definition Language (DDL)

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)
);

Data Manipulation Language (DML)

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');

Data Query Language (DQL)

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';

Data Control Language (DCL)

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;

Transaction Control Language (TCL)

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;

Advanced Topics

Joins

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

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);

Views

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

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;

Examples

Here are some practical examples to illustrate SQL commands:

Creating a Table

CREATE TABLE products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(100),
    price DECIMAL(10, 2)
);

Inserting Data

INSERT INTO products (product_id, product_name, price) 
VALUES (1, 'Laptop', 999.99);

Updating Data

UPDATE products 
SET price = 899.99 
WHERE product_id = 1;

Deleting Data

DELETE FROM products 
WHERE product_id = 1;

Contributing

Contributions are welcome! If you have suggestions or improvements, please create a pull request. You can also report issues or request new features.

License

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!

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •