{"id":20227563,"url":"https://github.com/k-sameer701/mysql-cheatsheet","last_synced_at":"2025-06-25T05:40:14.092Z","repository":{"id":216661808,"uuid":"741461336","full_name":"k-sameer701/MySql-CheatSheet","owner":"k-sameer701","description":"MySql CheatSheet is a condensed guide that provides a quick overview of essential SQL commands and their syntax for MySQL databases. It is a handy resource for developers.","archived":false,"fork":false,"pushed_at":"2024-01-11T12:39:33.000Z","size":106,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T23:41:03.488Z","etag":null,"topics":["mysql","mysqldata","mysqldatabase","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k-sameer701.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-10T12:53:06.000Z","updated_at":"2024-01-11T13:11:56.000Z","dependencies_parsed_at":"2024-01-11T20:46:26.347Z","dependency_job_id":"386c4d05-9dc5-46e1-926c-aeae8dd23c45","html_url":"https://github.com/k-sameer701/MySql-CheatSheet","commit_stats":null,"previous_names":["k-sameer701/mysql-cheatsheet"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-sameer701%2FMySql-CheatSheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-sameer701%2FMySql-CheatSheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-sameer701%2FMySql-CheatSheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-sameer701%2FMySql-CheatSheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k-sameer701","download_url":"https://codeload.github.com/k-sameer701/MySql-CheatSheet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241670154,"owners_count":20000327,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","mysqldata","mysqldatabase","sql"],"created_at":"2024-11-14T07:25:23.692Z","updated_at":"2025-03-03T13:15:22.721Z","avatar_url":"https://github.com/k-sameer701.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🐈‍⬛ MySql-CheatSheet\n\n## ⛄ Table of Content\n\n* [🌱 Introduction](#Introduction)\n* [🌱 Database-Table](#Database-Table)\n* [🌱 Inserting-Data](#Inserting-Data)\n* [🌱 Select-And-Where](#Select-And-Where)\n* [🌱 Constraints](#Constraints)\n* [🌱 AND-OR-NOT](#AND-OR-NOT)\n* [🌱 In-Operator](#In-Operator)\n* [🌱 LIKE-Operator](#LIKE-Operator)\n* [🌱 Between-NOT-Between](#Between-NOT-Between)\n* [🌱 Order-By-And-Distinct](#Order-By-And-Distinct)\n* [🌱 Is-Null-And-Is-Not-Null](#Is-Null-And-Is-Not-Null)\n* [🌱 Limit-And-Offset](#Limit-And-Offset)\n* [🌱 Aggregate-Functions](#Aggregate-Functions)\n* [🌱 Update-Query](#Update-Query)\n* [🌱 Delete-Query](#Delete-Query)\n* [🌱 Primary-And-Foreign-Key](#Primary-And-Foreign-Key)\n* [🌱 Joins](#Joins)\n* [🌱 Sub-Query](#Sub-Query)\n* [🌱 Exists-And-Not-Exists](#Exists-And-Not-Exists)\n\n## Introduction \n\n## ☘️ What is a database?\n    \n`A database is an organized collection of structured information or data.`\n\n## ☘️ What is DBMS?\n\n`Database Management Systems (DBMS) are software systems used to store, retrieve, and run queries on data.`\n\nIt is of two types:\n\n- `Relational (SQL)`\n    \n    A relational database is a collection of data items with pre-defined relationships between them, stored in the form of tables, rows, and columns.\n\n    Examples: MySQL, Oracle, PostgreSQL.\n\n- `Non-Relational (No-SQL)`\n\n    NoSQL databases are non-relational, offering flexible schemas, scalability, and support for diverse data models. Commonly used in web development, analytics, and content management for handling large and dynamic data.\n    \n    Examples: MongoDB, Cassandra, Redis. \n\n    ![--r](https://github.com/k-sameer701/Learn-MySql/assets/103239208/901affca-cdfb-4dc6-b431-8f197415267b)\n    \n## ☘️ What is MySQL?\n\n`MySQL is a relational database management system (RDBMS) developed by Oracle i.e. based on structured query language (SQL).`\n\n## Database-Table\n\n## ☘️ What is a database table?\n`A database table is a collection of rows and columns that contains relational data.`\n\n![table](https://github.com/k-sameer701/Learn-SQL/assets/103239208/4666a64d-aa02-4580-8261-f3ff67fc216a)\n\n### 👻 Create a Database:\n\n#### 🔥 Syntax:\n    CREATE DATABASE database_name;\n\n#### 👀 Example: \n    CREATE DATABASE mydatabase;\n\n### 👻 Switch to the Created Database:\n\n#### 🔥 Syntax: \n    USE database_name;\n\n#### 👀 Example: \n    USE mydatabase;\n\n###  👻 Create Tables:\nDefine table structure with columns and data types.\n\n#### 🔥 Syntax:\n    CREATE TABLE table_name \n    (\n        column1 datatype1,\n        column2 datatype2,\n        ...\n    );\n    \n#### 👀 Example:\n    CREATE TABLE users\n    (\n        id int unsigned,\n        name varchar(100),\n        email varchar(150),\n        password varchar(100),\n        contact varchar(15),\n        address text,\n        dob date,\n        gender enum(\"M\", \"F\", \"O\"),\n        status boolean\n    );\n\n#### 👻 Categories of DataTypes in SQL:\n\n- Strings\n- Numeric\n- Date \u0026 Time\n\n\n`💀 String Data Types`\n\n1. **CHAR(n):**\n   Fixed-length character string.\n\n2. **VARCHAR(n):**\n   Variable-length character string.\n\n3. **TEXT:**\n   Variable-length character string with no specified limit.\n\n4. **TINYTEXT:**\n    A very small variable-length character string.\n\n5. **MEDIUMTEXT:**\n   A medium-sized variable-length character string.\n\n6. **LONGTEXT:**\n   A long variable-length character string.\n\n7. **BINARY(n):**\n   Fixed-length binary string.\n\n8. **VARBINARY(n):**\n   Variable-length binary string.\n\n9. **BLOB:**\n   Variable-length binary large object.\n\n10. **TINYBLOB:**\n    A very small variable-length binary large object.\n\n11. **MEDIUMBLOB:**\n    A medium-sized variable-length binary large object.\n\n12. **LONGBLOB:**\n    A long variable-length binary large object.\n\n13. **ENUM('value1', 'value2', ...):**\n    Enumerated type, representing one of a predefined set of values.\n\n14. **SET('value1', 'value2', ...):**\n    Set type, representing a collection of values chosen from a predefined set.\n\n\n`💀 Number Data Types`\n\n1. **INTEGER or INT:**\n   Represents whole numbers.\n\n2. **TINYINT:**\n   A very small integer.\n\n3. **SMALLINT:**\n   Small-sized integer.\n\n4. **MEDIUMINT:**\n   Medium-sized integer.\n\n5. **BIGINT:**\n   Large-sized integer.\n\n6. **DECIMAL(p, s) or NUMERIC(p, s):**\n   Fixed-point decimal numbers with precision (total digits) and scale (decimal places).\n\n7. **FLOAT(p):**\n   Floating-point number with single-precision.\n\n8. **DOUBLE(p):**\n   Floating-point number with double-precision.\n\n9. **REAL:**\n   A synonym for DOUBLE.\n\n10. **BIT(n):**\n    A bit-field type, representing a fixed number of bits.\n\n`💀 Date-Time Data Types`\n\nIn MySQL, the specific date and time data types include:\n\n1. **DATE:**\n   Represents a date in the format YYYY-MM-DD.\n\n2. **TIME:**\n   Represents a time of day in the format HH:MM:SS.\n\n3. **DATETIME:**\n   Represents a combination of date and time in the format YYYY-MM-DD HH:MM:SS.\n\n4. **TIMESTAMP:**\n   Similar to DATETIME but with a narrower range and automatically converts to the current timestamp on insert or update.\n\n5. **YEAR:**\n   Represents a four-digit year value.\n\n## Inserting-Data\n\n## ☘️ Inserting Single Row\n\n    🔥 Syntax:\n\n        INSERT INTO table_name \n        (column1, column2, column3, ...)\n        VALUES \n        (value1, value2, value3, ...);\n\n    👀 Example:\n\n        INSERT INTO users \n        (id, name, email, password, contact, address, dob, gender, status)\n        VALUES \n        (1, 'John Doe', 'john@example.com', 'hashed_password', '+123456789', '123 Main St, City', '1990-01-15', 'M', 1);\n\n## ☘️ Inserting Multiple Rows\n\n    🔥 Syntax:\n\n        INSERT INTO table_name \n        (column1, column2, column3, ...)\n        VALUES \n        (value1, value2, value3, ...),\n        (value1, value2, value3, ...),\n        (value1, value2, value3, ...)\n\n    👀 Example:\n\n        INSERT INTO users \n        (id, name, email, password, contact, address, dob, gender, status)\n        VALUES\n        (2, 'Jane Smith', 'jane@example.com', 'hashed_password2', '+987654321', '456 Oak St, Town', '1995-03-22', 'F', 1),\n        (3, 'Alex Johnson', 'alex@example.com', 'hashed_password3', '+1122334455', '789 Pine St, Village', '1988-07-10', 'M', 0),\n        (4, 'Avery Taylor', 'avery@example.com', 'hashed_password4', '+9988776655', '321 Elm St, City', '2000-09-05', 'O', 1),\n        (5, 'Chris Anderson', 'chris@example.com', 'hashed_password5', '+1122334466', '567 Birch St, Town', '1992-12-18', 'M', 0),\n        (6, 'Taylor Davis', 'taylor@example.com', 'hashed_password6', '+1122334477', '876 Maple St, Village', '1985-05-30', 'F', 1);\n\n![Screenshot (2660)](https://github.com/k-sameer701/Learn-SQL/assets/103239208/2a40d253-4d40-4af2-8091-e3833db1cc21)\n\n## Select-And-Where\n\n## ☘️ SELECT Statement\n\n`In MySQL, you use the SELECT statement to retrieve data from one or more tables. `\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name;\n\n    👀 Example:\n\n        SELECT name, email\n        FROM users;\n\n\n    If you want to select all columns from a table, you can use the wildcard *\n\n    🔥 Syntax:\n\n        SELECT *\n        FROM table_name;\n\n    👀 Example:\n\n        SELECT *\n        FROM users;\n\n## ☘️ WHERE Clause\n\n`The WHERE clause is used to filter records.`\n\n`It is used to extract only those records that fulfill a specified condition.`\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE condition\n\n    👀 Example:\n        \n        SELECT id, name, email\n        FROM users\n        WHERE gender = 'M' AND status = 1;\n\n## ☘️ WHERE Conditions\n\n`In SQL, the WHERE clause is used to filter the rows returned by a query based on specified conditions. Here are common conditions that can be used in conjunction with the WHERE clause:`\n\n1. **Equality (`=`):**\n   - Used to filter rows where a specified column is equal to a specific value.\n   - Example: `WHERE column_name = value;`\n\n2. **Inequality (`!=` or `\u003c\u003e`):**\n   - Used to filter rows where a specified column is not equal to a specific value.\n   - Example: `WHERE column_name \u003c\u003e value;`\n\n3. **Comparison Operators (`\u003c`, `\u003e`, `\u003c=`, `\u003e=`):**\n   - Used to filter rows based on numerical or date comparisons.\n   - Example: `WHERE column_name \u003e value;`\n\n4. **LIKE Operator:**\n   - Used to perform pattern matching with wildcard characters (`%` for any sequence of characters, `_` for a single character).\n   - Example: `WHERE column_name LIKE 'pattern';`\n\n5. **BETWEEN Operator:**\n   - Used to filter rows within a specified range.\n   - Example: `WHERE column_name BETWEEN value1 AND value2;`\n\n6. **IN Operator:**\n   - Used to filter rows where a column matches any value in a specified list.\n   - Example: `WHERE column_name IN (value1, value2, ...);`\n\n7. **IS NULL and IS NOT NULL:**\n   - Used to filter rows where a column is (or is not) NULL.\n   - Example: `WHERE column_name IS NULL;`\n\n8. **Logical Operators (AND, OR, NOT):**\n   - Used to combine multiple conditions.\n   - Example: `WHERE condition1 AND condition2;`\n\n9. **EXISTS Operator:**\n   - Used to check if a subquery returns any results.\n   - Example: `WHERE EXISTS (SELECT * FROM another_table WHERE condition);`\n\n10. **ANY and ALL Operators:**\n    - Used with subqueries for comparing values.\n    - Example: `WHERE column_name \u003e ANY (SELECT column_name FROM another_table);`\n\n## ☘️ ALIAS\n\n`In SQL, an alias is a temporary name. It is used to provide a more readable or meaningful name to a table or column, especially when dealing with complex queries or when working with the result of a query.`\n\nThere are two main types of aliases:\n\n1. **Table Alias:**\n   - Used to give a table a temporary, alternative name in a query.\n\n   - 🔥 Syntax:\n     \n            SELECT column1, column2, ...\n            FROM table_name AS alias_name\n            WHERE condition;\n     \n   - 👀 Example:\n     \n            SELECT name, id\n            FROM users AS Active_Users\n            WHERE status = 1;\n\n2. **Column Alias:**\n   - Used to assign a temporary name to a column in the result set.\n   \n   - 🔥 Syntax:\n     \n            SELECT column_name AS alias_name, ...\n            FROM table_name\n            WHERE condition;\n            \n   - 👀 Example:\n     \n            SELECT name AS E_Name, id AS U_ID\n            FROM users \n            WHERE status = 1;\n     \n## Constraints\n\n## Table without Constraints 👇\n\n![Screenshot (2661)-1](https://github.com/k-sameer701/Learn-SQL/assets/103239208/2ffa2d83-07c7-4a09-9603-86e0e1d516cb)\n\n\nIn MySQL, table constraints are rules or conditions applied to the columns in a table to maintain the integrity, accuracy, and relationships of the data. \nThey are specified during the table creation using the `CREATE TABLE` statement. Constraints can also be added or modified later using the `ALTER TABLE` statement.\n\n1. **PRIMARY KEY Constraint:**\n   - Ensures that a column or a combination of columns uniquely identifies each row in the table.\n   - 👀 Example:\n     \n            CREATE TABLE employees (\n                employee_id INT PRIMARY KEY,\n                first_name VARCHAR(50),\n                last_name VARCHAR(50)\n            );\n\n            \n\n2. **FOREIGN KEY Constraint:**\n   - Establishes a link between two tables, enforcing referential integrity.\n   - 👀 Example:\n     \n            CREATE TABLE orders (\n                order_id INT PRIMARY KEY,\n                customer_id INT,\n                FOREIGN KEY (customer_id) REFERENCES customers(customer_id)\n            );\n            \n\n3. **UNIQUE Constraint:**\n   - Ensures that all values in a column (or a combination of columns) are unique.\n   - 👀 Example:\n     \n            CREATE TABLE products (\n                product_name VARCHAR(100) UNIQUE,\n                price DECIMAL(10, 2)\n            );\n\n            \n\n4. **CHECK Constraint:**\n   - Specifies a condition that must be satisfied for each row in the table.\n   - 👀 Example:\n     \n            CREATE TABLE employees (\n                employee_id INT,\n                salary DECIMAL(10, 2) CHECK (salary \u003e= 0)\n            );\n            \n\n5. **DEFAULT Constraint:**\n   - Provides a default value for a column if no value is specified during an INSERT operation.\n   - 👀 Example:\n     \n            CREATE TABLE students\n            (\n                id INT NOT NULL UNIQUE,\n                name VARCHAR(100) NOT NULL,\n                email VARCHAR(150) NOT NULL UNIQUE,\n                age TINYINT CHECK (age \u003e= 18),\n                status BOOLEAN DEFAULT 1\n            );\n            \n\n6. **NOT NULL Constraint:**\n   - Ensures that a column cannot have a NULL value.\n   - 👀 Example:\n     \n            CREATE TABLE orders (\n                order_id INT,\n                order_date DATE NOT NULL\n            );\n\n\n## AND-OR-NOT\n\n1. **AND Operator:**\n   - The `AND` operator is used to retrieve rows that satisfy multiple conditions simultaneously.\n\n   - 👀 Example: Retrieve students who are both active (`status = 1`) and have an age greater than or equal to 20.\n     \n            SELECT * \n            FROM students \n            WHERE status = 1 AND age \u003e= 20;\n     \n\n2. **OR Operator:**\n   - The `OR` operator is used to retrieve rows that satisfy at least one of multiple conditions.\n\n   - 👀 Example: Retrieve students who are either inactive (`status = 0`) or have an age less than 18.\n   \n          SELECT * \n          FROM students \n          WHERE status = 0 OR age \u003c 18;\n     \n\n3. **NOT Operator:**\n   - The `NOT` operator is used to negate a condition, retrieving rows that do not satisfy a particular condition.\n   - 👀 Example: Retrieve students who are not active (`status = 0`).\n     \n            SELECT * \n            FROM students \n            WHERE NOT status = 0;\n\n## In-Operator\n\n## ☘️ IN Operator\n\n`The SQL IN operator is used to specify multiple values in a WHERE clause for a SELECT, UPDATE, DELETE, or INSERT statement. It is typically used to reduce the use of multiple OR conditions in a query.`\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name IN (value1, value2, ...);\n\n\n- `column1, column2, ...`: The columns you want to retrieve in the SELECT statement.\n- `table_name`: The name of the table from which to retrieve the data.\n- `column_name`: The column against which you want to compare values.\n- `(value1, value2, ...)`: The list of values to compare with the specified column.\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        WHERE age IN (19,20);\n\n## LIKE-Operator\n\n## ☘️ LIKE Operator\n\nThe LIKE Operator is used in a WHERE Clause to search for a specified pattern in a column.\n\n- The percent sign (%) represents zero, one, or multiple characters.\n- The underscore sign (_) represents one, single character.\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name LIKE pattern;\n\n    👀 Example:\n\n        SELECT name\n        FROM students\n        WHERE name LIKE \"a%\";\n\n## Patterns\n\n    'a%'    :   Starts with \"a\".\n    '%a'    :   End with \"a\".\n    '%or%'  :   Have \"or\" in any position.\n    '_r%'   :   Have \"r\" in the second position.\n    'a_%'   :   Starts with \"a\" and are at least 2 characters in length (including a).\n    'a__%'  :   Starts with \"a\" and are at least 3 characters in length (including a).\n    'a%o'   :   Starts with \"a\" and ends with \"o\".\n\n\n## Between-NOT-Between\n\n## ☘️ BETWEEN \u0026 NOT BETWEEN\n\n`The BETWEEN and NOT BETWEEN operators in SQL are used to filter the result set based on a range of values. These operators are typically used in the WHERE clause of a SQL query.`\n\n    🔥 Syntax (BETEEN):\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name BETWEEN value1 AND value2;\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        WHERE age BETWEEN 20 AND 30;\n\n    🔥 Syntax (NOT BETWEEN):\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name NOT BETWEEN value1 AND value2;\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        WHERE age NOT BETWEEN 20 AND 30;\n\n## Order-By-And-Distinct\n\n## ☘️ ORDER BY\n\n`The ORDER BY clause in SQL is used to sort the result set of a query based on one or more columns. It is typically used at the end of a SELECT statement.`\n\n`Remember that you can sort in ascending order (ASC) or descending order (DESC). If you don't specify the order, ASC is assumed by default. `\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE condition\n        ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;\n\n    👀 Example :\n\n        SELECT id, name, age, status\n        FROM students\n        ORDER BY age ASC;\n\n## ☘️ DISTINCT\n\n`The DISTINCT keyword in SQL is used in a SELECT statement to eliminate duplicate rows from the result set. It returns only unique values for the specified columns.`\n\n    🔥 Syntax:\n\n        SELECT DISTINCT column1, column2, ...\n        FROM table_name\n        WHERE condition;\n\n    👀 Example:\n\n        SELECT DISTINCT status\n        FROM students;\n\n## Is-Null-And-Is-Not-Null\n\n## ☘️ IS NULL\n\n`The IS NULL operator is used to filter rows where a specified column contains a null value.`\n\n    🔥 Syntax:\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name IS NULL;\n\n    👀 Example:\n        SELECT *\n        FROM students\n        WHERE age IS NULL;\n\n\n## ☘️ IS NOT NULL\n\n`The IS NOT NULL operator is used to filter rows where a specified column does not contain a null value.`\n\n    🔥 Syntax:\n\n        SELECT column1, column2, ...\n        FROM table_name\n        WHERE column_name IS NOT NULL;\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        WHERE age IS NOT NULL;\n\n## Limit-And-Offset\n\n## ☘️ LIMIT\n\n`If want to LIMIT, the number of results that are returned you can simply use the LIMIT command with several rows to LIMIT by.`\n\n    🔥 Syntax:\n\n        SELECT *\n        FROM table_name\n        LIMIT number_to_limit_by;\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        LIMIT 3;\n\n## ☘️ OFFSET\n\n`You can also specify an OFFSET from where to start returning the Data.`\n\n    🔥 Syntax:\n\n        SELECT *\n        FROM table_name\n        LIMIT number_to_limit_by OFFSET number_of_rows_to_skip;\n\n    👀 Example:\n\n        SELECT *\n        FROM students\n        LIMIT 3 OFFSET 2;\n\n## Aggregate-Functions\n\n## ☘️ AGGREGATE FUNCTIONS\n\n`In MySQL, aggregate functions are used to perform a calculation on a set of values and return a single value.`\n\nHere are some common aggregate functions in MySQL:\n\n1. **COUNT():**\n   - Counts the number of rows in a result set.\n    \n    🔥 Syntax:\n    \n        SELECT COUNT(column_name) FROM table_name WHERE condition;\n\n\n2. **SUM():**\n   - Calculates the sum of values in a numeric column.\n    \n    🔥 Syntax:\n        \n        SELECT SUM(column_name) FROM table_name WHERE condition;\n   \n\n3. **AVG():**\n   - Calculates the average of values in a numeric column.\n\n    🔥 Syntax:\n        \n        SELECT AVG(column_name) FROM table_name WHERE condition;\n   \n\n4. **MIN():**\n   - Finds the minimum value in a column.\n\n    🔥 Syntax:\n    \n        SELECT MIN(column_name) FROM table_name WHERE condition;\n   \n\n5. **MAX():**\n   - Finds the maximum value in a column.\n   \n    🔥 Syntax:\n    \n        SELECT MAX(column_name) FROM table_name WHERE condition;\n   \n\n    👀 Example:\n\n        SELECT\n        SUM() AS total_sales,\n        AVG(sale_amount) AS average_sale,\n        COUNT(order_id) AS order_count\n        FROM sales\n        WHERE sale_date \u003e= '2022-01-01';\n\n\n## Update-Query\n\n## 🌱 UPDATE \n\n- `An update query is a database operation that modifies existing records in a table.`\n- `Specify a condition that determines which records to update. If you omit the WHERE clause, all records in the table will be updated.`\n\n    🔥 Syntax:\n\n        UPDATE table_name\n        SET column1 = value1, column2 = value2, ...\n        WHERE condition;\n\n    👀 Example:\n\n        UPDATE employees\n        SET salary = salary * 1.1\n        WHERE bonus \u003e 5000;\n\n## Delete-Query\n\n## ☘️ DELETE \n\n- `A delete query is used in SQL to remove records from a table based on specified conditions.`\n- `Please be careful when using delete queries, especially without a WHERE clause, as it can result in the removal of all records from the specified table. `\n\n    🔥 Syntax:\n\n        DELETE FROM table_name\n        WHERE condition;\n\n    👀 Example:\n\n        DELETE FROM employees\n        WHERE salary \u003c 30000;\n\n## Primary-And-Foreign-Key\n\n## ☘️ PRIMARY KEY\n\n- `A primary key is a unique identifier for a record in a database table.`\n- `It must contain unique values and cannot have NULL values.`\n- `Each table in a relational database typically has a primary key that uniquely identifies each record in that table.`\n- `The primary key is used to establish relationships between tables.`\n\n\n    👀 Example\n\n        CREATE TABLE cities \n        (\n            city_id INT PRIMARY KEY,\n            city_name VARCHAR(50),\n            country VARCHAR(50)\n        );\n\n        INSERT INTO cities (city_id, city_name, country)\n        VALUES\n        (1, 'New York', 'USA'),\n        (2, 'London', 'UK'),\n        (3, 'Tokyo', 'Japan'),\n        (4, 'Paris', 'France');\n\n## ☘️ FOREIGN KEY:\n\n- `A foreign key is a field in a database table that is a primary key in another table.`\n- `It establishes a link between the data in two tables.`\n- `The foreign key in one table points to the primary key in another table.`\n- `It is used to maintain referential integrity between the two related tables.`\n\n    👀 Example\n\n        CREATE TABLE students \n        (\n            student_id INT PRIMARY KEY,\n            student_name VARCHAR(50),\n            age INT,\n            city_id INT,\n            FOREIGN KEY (city_id) REFERENCES cities(city_id)\n        );\n\n        INSERT INTO students (student_id, student_name, age, city_id)\n        VALUES\n        (101, 'John Doe', 20, 1), \n        (102, 'Jane Smith', 22, 2), \n        (103, 'Ken Yamada', 21, 3), \n        (104, 'Emma Johnson', 23, 4);\n\n## Joins\n\n## ☘️ Join Query\n\n`Joins are operations that combine rows from two or more tables based on a related column between them. The purpose of joins is to retrieve data from multiple tables in a single result set.`\n\n![TABLE 1](https://github.com/k-sameer701/Learn-SQL/assets/103239208/5429a2a5-7c29-436a-92b5-908ece2aabe2)\n\n\n**1. Inner Join:**\n- Returns only the rows where there is a match in both tables based on the specified condition.\n\n        🔥 Syntax\n            SELECT *\n            FROM table1\n            INNER JOIN table2 ON table1.column = table2.column;\n\n        👀 Example\n            SELECT *\n            FROM students\n            INNER JOIN cities ON students.city_id = cities.city_id;\n\n\n**2. Left Join (or Left Outer Join):**\n- Returns all rows from the left table (`students`) and the matched rows from the right table (`cities`). If there is no match, NULL values are returned for columns from the right table.\n\n        🔥 Syntax\n            SELECT *\n            FROM table1\n            LEFT JOIN table2 ON table1.column = table2.column;\n\n        👀 Example\n            SELECT *\n            FROM students\n            LEFT JOIN cities ON students.city_id = cities.city_id;\n\n\n**3. Right Join (or Right Outer Join):**\n- Returns all rows from the right table (`cities`) and the matched rows from the left table (`students`). If there is no match, NULL values are returned for columns from the left table.\n\n        🔥 Syntax:\n            SELECT *\n            FROM table1\n            RIGHT JOIN table2 ON table1.column = table2.column;\n\n        👀 Example\n            SELECT *\n            FROM students\n            RIGHT JOIN cities ON students.city_id = cities.city_id;\n\n**4. Cross Join:**\n- Returns the Cartesian product of both tables, meaning every row from the left table is combined with every row from the right table.\n\n        🔥 Syntax:\n            SELECT *\n            FROM table1\n            CROSS JOIN table2;\n\n        👀 Example\n            SELECT *\n            FROM students\n            CROSS JOIN cities;\n\n## Sub-Query\n\n# ☘️ Sub Query\n\n`A subquery, also known as an inner query or nested query, is a query within another SQL query. Subqueries are enclosed in parentheses and can be used in various parts of a SQL statement, such as the SELECT, FROM, WHERE, and HAVING clauses.`\n\n    🔥 Syntax\n    \n        SELECT column_name\n        FROM table_name\n        WHERE\n        (SELECT column_name FROM table_name)\n\n    👀 Example\n\n        SELECT student_name\n        FROM students\n        WHERE age \u003e (SELECT AVG(age) FROM students);\n\n## Exists-And-Not-Exists\n\n## ☘️ EXISTS:\n\n`The EXISTS operator is used to check if a subquery returns any rows. If the subquery returns at least one row, the condition is considered to be true, and the outer query continues.`\n\n    🔥 Syntax:\n       \n        SELECT column_name\n        FROM table1\n        WHERE EXISTS\n        (SELECT column_name FROM table_name2);\n\n    👀 Example:\n\n        SELECT name\n        FROM students\n        WHERE EXISTS\n        (SELECT id FROM cities WHERE name = 'London');\n\n## ☘️ NOT EXISTS:\n\n`The NOT EXISTS operator is the opposite of EXISTS. It is used to check if a subquery returns no rows. If the subquery returns no rows, the condition is considered to be true, and the outer query continues.`\n\n    🔥 Syntax\n    \n        SELECT column_name\n        FROM table1\n        WHERE NOT EXISTS\n        (SELECT column_name FROM table_name2);\n\n    👀 Example\n\n        SELECT name\n        FROM students\n        WHERE NOT EXISTS\n        (SELECT id FROM cities WHERE name = 'London');\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-sameer701%2Fmysql-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk-sameer701%2Fmysql-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-sameer701%2Fmysql-cheatsheet/lists"}