0% found this document useful (0 votes)
17 views

SQL Cheatsheet

SQL cheat sheet

Uploaded by

aslamworld18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Cheatsheet

SQL cheat sheet

Uploaded by

aslamworld18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ANSI SQL through MySQL

1. DDL (Data Defini on Language)


SQL
Statement Explanation Syntax Example
CREATE TABLE CREATE TABLE Employees
Creates a new table_name (column1 (EmployeeID INT PRIMARY KEY,
database object datatype, column2 FirstName VARCHAR(50), LastName
CREATE (e.g., table, index) datatype, ...); VARCHAR(50));
Modifies the
structure of an ALTER TABLE
existing database table_name ADD ALTER TABLE Employees ADD
ALTER object column_name datatype; Email VARCHAR(100);
Deletes an existing DROP TABLE
DROP database object table_name; DROP TABLE Employees;
Removes all data TRUNCATE TABLE
TRUNCATE from a table table_name; TRUNCATE TABLE Employees;
Changes the name
of an existing ALTER TABLE old_name ALTER TABLE Employees RENAME
RENAME database object RENAME TO new_name; TO Staff;

2. DML(Data manipula on language)


SQL
Statement Explanation Syntax Example
SELECT column1,
Retrieves data column2 FROM
from one or more table_name WHERE SELECT FirstName, LastName FROM
SELECT tables condition; Employees WHERE DepartmentID = 1;
INSERT INTO
table_name (column1, INSERT INTO Employees (EmployeeID,
Adds new data column2) VALUES FirstName, LastName) VALUES (1,
INSERT into a table (value1, value2); 'John', 'Doe');
UPDATE table_name UPDATE Employees SET Email =
Modifies existing SET column1 = value1 'john.doe@example.com' WHERE
UPDATE data in a table WHERE condition; EmployeeID = 1;
DELETE FROM
Removes data table_name WHERE DELETE FROM Employees WHERE
DELETE from a table condition; EmployeeID = 1;
3. DCL (Data Control Language)
SQL
Statement Explanation Syntax Example
Gives specific GRANT privilege_name ON GRANT SELECT, INSERT ON
GRANT privileges to a user object_name TO user; Employees TO 'user'@'localhost';
REVOKE privilege_name REVOKE INSERT ON
Removes specific ON object_name FROM Employees FROM
REVOKE privileges from a user user; 'user'@'localhost';

4. Transac on Control Language


SQL Statement Explanation Syntax Example
BEGIN; UPDATE Accounts
Saves the transaction SET Balance = Balance - 100
COMMIT permanently COMMIT; WHERE ID = 1; COMMIT;
BEGIN; UPDATE Accounts
Undoes the current SET Balance = Balance - 100
ROLLBACK transaction ROLLBACK; WHERE ID = 1; ROLLBACK;
Creates a point within
a transaction to SAVEPOINT
SAVEPOINT ROLLBACK to savepoint_name; SAVEPOINT sp1;
Specifies SET TRANSACTION
SET characteristics for the [READ ONLY/READ SET TRANSACTION READ
TRANSACTION transaction WRITE]; ONLY;
5. Constraints
Constraint Explanation Syntax Example
Uniquely
identifies each column_name datatype EmployeeID INT PRIMARY
PRIMARY KEY record in a table PRIMARY KEY KEY
FOREIGN KEY
(column_name) FOREIGN KEY (DepartmentID)
Links two tables REFERENCES REFERENCES
FOREIGN KEY together other_table(column_name) Departments(DepartmentID)
Ensures all
values in a
column are column_name datatype Email VARCHAR(100)
UNIQUE different UNIQUE UNIQUE
Ensures a column
cannot have a column_name datatype NOT FirstName VARCHAR(50) NOT
NOT NULL NULL value NULL NULL
Ensures all
values in a
column satisfy a
CHECK specific condition CHECK (condition) CHECK (Age >= 18)
Sets a default
value for a column_name datatype HireDate DATE DEFAULT
DEFAULT column DEFAULT default_value CURRENT_DATE

6. SQL Operators
Operator
Type Operators Explanation Example
Perform mathematical SELECT Price, Price * 1.1 AS
Arithmetic +, -, *, /, % operations PriceWithTax FROM Products;
=, <>, !=, >, <, SELECT * FROM Orders WHERE
Comparison >=, <= Compare two values TotalAmount > 1000;
SELECT * FROM Employees WHERE
Combine multiple Department = 'Sales' AND Salary >
Logical AND, OR, NOT conditions 50000;
Check if a value is within SELECT * FROM Products WHERE Price
BETWEEN BETWEEN a range BETWEEN 10 AND 20;
Pattern matching with SELECT * FROM Employees WHERE
LIKE LIKE wildcard characters LastName LIKE 'S%';
Specify multiple values in SELECT * FROM Orders WHERE Status
IN IN a WHERE clause IN ('Shipped', 'Delivered');
SELECT * FROM Customers WHERE
IS NULL IS NULL Check for null values Phone IS NULL;
7. SQL Func ons
Function
Type Examples Explanation Example Usage
CONCAT(), SELECT CONCAT(FirstName, ' ',
String SUBSTRING(), UPPER(), Manipulate string LastName) AS FullName FROM
Functions LOWER() data Employees;
Perform
Numeric ROUND(), ABS(), CEIL(), operations on SELECT ROUND(Price, 2) AS
Functions FLOOR() numeric data RoundedPrice FROM Products;
NOW(), CURDATE(), SELECT OrderID,
Date DATEDIFF(), Manipulate date DATEDIFF(CURDATE(), OrderDate)
Functions DATE_FORMAT() and time data AS DaysSinceOrder FROM Orders;
Perform
Aggregate COUNT(), SUM(), AVG(), calculations on a SELECT AVG(Salary) AS
Functions MAX(), MIN() set of values AverageSalary FROM Employees;

8. SQL CLAUSES
Clause Explanation Syntax Example

SELECT * FROM Employees


Filters records based SELECT column1, column2 FROM WHERE Department =
WHERE on a condition table_name WHERE condition; 'Sales';

SELECT Department,
SELECT column1, COUNT(*) FROM
GROUP Groups rows that have aggregate_function(column2) FROM Employees GROUP BY
BY the same values table_name GROUP BY column1; Department;

SELECT Department,
SELECT column1, AVG(Salary) FROM
aggregate_function(column2) FROM Employees GROUP BY
Specifies a search table_name GROUP BY column1 HAVING Department HAVING
HAVING condition for a group condition; AVG(Salary) > 50000;

SELECT column1, column2 FROM


ORDER table_name ORDER BY column1 SELECT * FROM Employees
BY Sorts the result set [ASC/DESC]; ORDER BY LastName ASC;

Specifies the maximum SELECT * FROM Products


number of records to SELECT column1, column2 FROM ORDER BY Price DESC
LIMIT return table_name LIMIT number; LIMIT 10;

SELECT DISTINCT
Returns only distinct SELECT DISTINCT column1, column2 Department FROM
DISTINCT (different) values FROM table_name; Employees;
9. JOIN
Join
Type Explanation Syntax Example

SELECT * FROM
table1 INNER JOIN
Returns records that table2 ON SELECT o.OrderID, c.CustomerName
INNER have matching values table1.column = FROM Orders o INNER JOIN Customers c
JOIN in both tables table2.column; ON o.CustomerID = c.CustomerID;

SELECT * FROM
Returns all records table1 LEFT JOIN SELECT e.EmployeeID,
from the left table, table2 ON d.DepartmentName FROM Employees e
LEFT and matched records table1.column = LEFT JOIN Departments d ON
JOIN from the right table table2.column; e.DepartmentID = d.DepartmentID;

SELECT * FROM
Returns all records table1 RIGHT JOIN
from the right table, table2 ON SELECT o.OrderID, p.ProductName
RIGHT and matched records table1.column = FROM Orders o RIGHT JOIN Products p
JOIN from the left table table2.column; ON o.ProductID = p.ProductID;

SELECT * FROM
Returns all records table1 FULL JOIN SELECT * FROM Employees FULL JOIN
when there's a match table2 ON Departments ON
FULL in either left or right table1.column = Employees.DepartmentID =
JOIN table table2.column; Departments.DepartmentID;

SELECT * FROM SELECT e.FirstName, d.DepartmentName


CROSS Returns the Cartesian table1 CROSS JOIN FROM Employees e CROSS JOIN
JOIN product of both tables table2; Departments d;
10. SUBQUERY
Subquery
Location Explanation Example

Used to return a single SELECT OrderID, (SELECT CustomerName FROM


In SELECT value to be used in the Customers WHERE Customers.CustomerID =
clause outer SELECT statement Orders.CustomerID) AS CustomerName FROM Orders;

Used to return a virtual SELECT AVG(OrderTotal) FROM (SELECT CustomerID,


In FROM table to be used in the SUM(TotalAmount) AS OrderTotal FROM Orders
clause outer query GROUP BY CustomerID) AS CustomerOrderTotals;

Used to return data that will SELECT ProductName FROM Products WHERE
In WHERE be used in the WHERE ProductID NOT IN (SELECT DISTINCT ProductID FROM
clause condition OrderDetails);

Used with aggregate SELECT DepartmentID, AVG(Salary) FROM Employees


In HAVING functions in a GROUP BY GROUP BY DepartmentID HAVING AVG(Salary) >
clause clause (SELECT AVG(Salary) FROM Employees);

11. VIEWS
Operation Explanation Syntax Example

CREATE VIEW
HighValueCustomers AS
CREATE VIEW view_name SELECT CustomerID,
Creates a virtual table AS SELECT column1, CustomerName FROM
based on the result of a column2 FROM table_name Customers WHERE
CREATE VIEW SELECT statement WHERE condition; TotalOrders > 10000;

Uses a view like a SELECT * FROM


USE VIEW regular table SELECT * FROM view_name; HighValueCustomers;

CREATE OR REPLACE VIEW CREATE OR REPLACE


Updates a view (if it's view_name AS SELECT VIEW cust AS SELECT
UPDATE VIEW updatable) column name from table; name from customer;

DROP VIEW
DROP VIEW Deletes a view DROP VIEW view_name; HighValueCustomers;
12. INDICES
Operation Explanation Syntax Example

CREATE INDEX index_name


CREATE Creates an index ON table_name (column1, CREATE INDEX idx_lastname
INDEX on a table column2, ...); ON Employees(LastName);

CREATE CREATE UNIQUE INDEX


UNIQUE Creates a unique index_name ON table_name CREATE UNIQUE INDEX
INDEX index on a table (column1, column2, ...); idx_email ON Employees(Email);

Removes an DROP INDEX index_name DROP INDEX idx_lastname ON


DROP INDEX index from a table ON table_name; Employees;

Displays the SHOW INDEX FROM SHOW INDEX FROM


SHOW INDEX indexes on a table table_name; Employees;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy