Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
113 views

SQL Cheatsheet PDF

This document provides a summary of common SQL queries, operators, and functions. It covers basic SELECT queries to retrieve and filter data, JOIN operations to combine data from multiple tables, aggregation functions like COUNT and AVING to group and summarize data, DML commands for data manipulation including INSERT, UPDATE, and DELETE, and DDL commands to define and modify database schema through CREATE, ALTER, and DROP statements. It also includes an overview of constraints, views, and triggers that can be used to control data integrity and trigger stored procedures during DML operations.

Uploaded by

shubhangivs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
113 views

SQL Cheatsheet PDF

This document provides a summary of common SQL queries, operators, and functions. It covers basic SELECT queries to retrieve and filter data, JOIN operations to combine data from multiple tables, aggregation functions like COUNT and AVING to group and summarize data, DML commands for data manipulation including INSERT, UPDATE, and DELETE, and DDL commands to define and modify database schema through CREATE, ALTER, and DROP statements. It also includes an overview of constraints, views, and triggers that can be used to control data integrity and trigger stored procedures during DML operations.

Uploaded by

shubhangivs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Cheatsheet

Basic Queries & Operators JOINs Order, Group, Aggregate

SELECT c1, c2 FROM t; SELECT c1, c2 SELECT c1, c2 FROM t


Query data in columns c1, c2 from a table. FROM t1 ORDER BY c1 [ASC][DESC];
INNER JOIN t2 ON condition; Sort the results in ascending or descending order.
SELECT * FROM t;
Inner join t1 and t2
Query all rows and columns from a table SELECT c1, aggregate(c2)
FROM t
SELECT c1, c2 FROM t SELECT c1, c2 GROUP BY c1;
WHERE condition; FROM t1 Group rows using an aggregate function.
Query data and filter rows using a boolean condition: =, <, LEFT JOIN t2 ON condition;
<=, >, >=, <>. Left join t1 and t2 SELECT c1, aggregate(c2)
FROM t
SELECT c1, c2 FROM t1
GROUP BY c1;
WHERE c1[NOT] LIKE pattern; SELECT c1, c2
HAVING condition;
Query rows using pattern matching.  Use with % or _  FROM t1
Filter groups using HAVING operator. 
RIGHT JOIN t2 ON condition;
SELECT c1, c2 FROM t
Right join t1 and t2
WHERE c1 [NOT] IN value_list;
AGGREGATE FUNCTIONS
Filter rows with values equals to those in the value_list.
SELECT c1, c2 AVG returns the average of a list
SELECT c1, c2 FROM t FROM t1 COUNT returns the number of elements of a list
WHERE c1 BETWEEN limit1 AND limit2; FULL OUTER JOIN t2 ON condition; SUM returns the total of a list
Filter rows with values between the two limits. Full outer join t1 and t2 MAX returns the maximum value in a list
SELECT c1, c2 FROM t MIN returns the minimum value in a list
SELECT c1, c2
WHERE c1 IS [NOT] NULL;
FROM t1
Filter NULL values.
CROSS JOIN t2;
SELECT DISTINCT c1 FROM t Cross join t1 and t2. Results also called: Cartesian Product.
WHERE condition;
Returns distinct rows from a table
SELECT c1, c2
SELECT c1, c2 FROM t FROM t1 A
LIMIT n; INNER JOIN t1 B ON condition;
Returns the first n rows. Join t1 to itself using INNER JOIN. Also called: SELF JOIN.
SQL Cheatsheet
DML - Data Manipulation
DDL - Data Definition Language Constraints, Views, Triggers
Language
CREATE TABLE t ( INSERT INTO t(column_list) CONSTRAINTS DEFINITION
    id INT PRIMARY KEY, VALUES (value_list);
CREATE TABLE t1(
    c1 VARCHAR NOT NULL, Insert one record into a table.
      c1 INT PRIMARY KEY, -- primary key constraint
    c2 INT 
      c2 INT NOT NULL, -- NOT NULL constraint
); INSERT INTO t1(column_list)
      FOREIGN KEY (c2) REFERENCES t2(c2), -- Foreign Key
Createa new table with three columns SELECT column_list
      c3 INT,
FROM t2;
      UNIQUE(c3), -- UNIQUE constraint
DROP TABLE t ; Insert rows from table t2 into table t1. Columns types much
      CHECK (c3> 0 AND c3 >= c2) -- CHECK constraint
Delete table from the database match. 
);
ALTER TABLE t ADD column; UPDATE t
VIEWS 
Add a new column to the table SET c1= new_value,
       c2 = new_value CREATE [TEMPORARY] VIEW v(c1,c2)
ALTER TABLE t1 RENAME c1 TO c2;        /*c3, c4, ... */; AS
Rename column c1 to c2 Update values in the column c1 and c2 for all rows. SELECT c1, c2
FROM t;
ALTER TABLE t DROP COLUMN c ; UPDATE t Create a new view that consists of two columns from table t.
Remove column c from the table SET c1 = new_value,
DROP VIEW v;
       c2 = new_value
Delete the view
ALTER TABLE t RENAME TO tt ; WHERE condition;
Rename a table from t to tt Update values in the column c1, c2 that match the condition
TRIGGERS
TRUNCATE TABLE t; DELETE FROM t; CREATE [OR ALTER] TRIGGER trigger_name
Remove all data in a table Delete all data in a table BEFORE [OR AFTER] EVENT
ON table_name FOR EACH ROW [OR STATEMENT]
DELETE FROM t EXECUTE stored_procedure;
WHERE condition; Create or modify a trigger.
Delete rows that match the condition. EVENT values: INSERT, UPDATE, DELETE

DROP TRIGGER tr;


Delete a specific tr.

You might also like