SQL Cheat Sheet Download PDF it in PDF or PNG Format (2024)

The SQL cheat sheet provides you with the most commonly used SQL statements for your reference. You can download the SQL cheat sheet as follows:

Download 3-page SQL cheat sheet in PDF format

SQL Cheat Sheet Download PDF it in PDF or PNG Format (1)

SQL Cheat Sheet Download PDF it in PDF or PNG Format (2) SQL Cheat Sheet Download PDF it in PDF or PNG Format (3)

Querying data from a table

Query data in columns c1, c2 from a table

SELECT c1, c2 FROM t;Code language: SQL (Structured Query Language) (sql)

Query all rows and columns from a table

SELECT * FROM t;Code language: SQL (Structured Query Language) (sql)

Query data and filter rows with a condition

SELECT c1, c2 FROM tWHERE condition;Code language: SQL (Structured Query Language) (sql)

Query distinct rows from a table

SELECT DISTINCT c1 FROM tWHERE condition;Code language: SQL (Structured Query Language) (sql)

Sort the result set in ascending or descending order

SELECT c1, c2 FROM tORDER BY c1 ASC [DESC];Code language: SQL (Structured Query Language) (sql)

Skip offset of rows and return the next n rows

SELECT c1, c2 FROM tORDER BY c1 LIMIT n OFFSET offset;Code language: SQL (Structured Query Language) (sql)

Group rows using an aggregate function

SELECT c1, aggregate(c2)FROM tGROUP BY c1;Code language: SQL (Structured Query Language) (sql)

Filter groups using HAVING clause

SELECT c1, aggregate(c2)FROM tGROUP BY c1HAVING condition;Code language: SQL (Structured Query Language) (sql)

Querying from multiple tables

Inner join t1 and t2

SELECT c1, c2 FROM t1INNER JOIN t2 ON condition;Code language: SQL (Structured Query Language) (sql)

Left join t1 and t1

SELECT c1, c2 FROM t1LEFT JOIN t2 ON condition;Code language: SQL (Structured Query Language) (sql)

Right join t1 and t2

SELECT c1, c2 FROM t1RIGHT JOIN t2 ON condition;Code language: SQL (Structured Query Language) (sql)

Perform full outer join

SELECT c1, c2 FROM t1FULL OUTER JOIN t2 ON condition;Code language: SQL (Structured Query Language) (sql)

Produce a Cartesian product of rows in tables

SELECT c1, c2 FROM t1CROSS JOIN t2;Code language: SQL (Structured Query Language) (sql)

Another way to perform cross join

SELECT c1, c2 FROM t1, t2;Code language: SQL (Structured Query Language) (sql)

Join t1 to itself using INNER JOIN clause

SELECT c1, c2FROM t1 AINNER JOIN t1 B ON condition;Code language: SQL (Structured Query Language) (sql)

Using SQL Operators

Combine rows from two queries

SELECT c1, c2 FROM t1UNION [ALL]SELECT c1, c2 FROM t2;Code language: SQL (Structured Query Language) (sql)

Return the intersection of two queries

SELECT c1, c2 FROM t1INTERSECTSELECT c1, c2 FROM t2;Code language: SQL (Structured Query Language) (sql)

Subtract a result set from another result set

SELECT c1, c2 FROM t1MINUSSELECT c1, c2 FROM t2;Code language: SQL (Structured Query Language) (sql)

Query rows using pattern matching %, _

SELECT c1, c2 FROM t1WHERE c1 [NOT] LIKE pattern;Code language: SQL (Structured Query Language) (sql)

Query rows in a list

SELECT c1, c2 FROM tWHERE c1 [NOT] IN value_list;Code language: SQL (Structured Query Language) (sql)

Query rows between two values

SELECT c1, c2 FROM tWHERE c1 BETWEEN low AND high;Code language: SQL (Structured Query Language) (sql)

Check if values in a table is NULL or not

SELECT c1, c2 FROM tWHERE c1 IS [NOT] NULL;Code language: SQL (Structured Query Language) (sql)

Managing tables

Create a new table with three columns

CREATE TABLE t ( id INT PRIMARY KEY, name VARCHAR NOT NULL, price INT DEFAULT 0);Code language: SQL (Structured Query Language) (sql)

Delete the table from the database

DROP TABLE t ;Code language: SQL (Structured Query Language) (sql)

Add a new column to the table

ALTER TABLE t ADD column;Code language: SQL (Structured Query Language) (sql)

Drop column c from the table

ALTER TABLE t DROP COLUMN c ;Code language: SQL (Structured Query Language) (sql)

Add a constraint

ALTER TABLE t ADD constraint;Code language: SQL (Structured Query Language) (sql)

Drop a constraint

ALTER TABLE t DROP constraint;Code language: SQL (Structured Query Language) (sql)

Rename a table from t1 to t2

ALTER TABLE t1 RENAME TO t2;Code language: SQL (Structured Query Language) (sql)

Rename column c1 to c2

ALTER TABLE t1 RENAME c1 TO c2 ;Code language: SQL (Structured Query Language) (sql)

Remove all data in a table

TRUNCATE TABLE t;Code language: SQL (Structured Query Language) (sql)

Using SQL constraints

Set c1 and c2 as a primary key

CREATE TABLE t( c1 INT, c2 INT, c3 VARCHAR, PRIMARY KEY (c1,c2));Code language: SQL (Structured Query Language) (sql)

Set c2 column as a foreign key

CREATE TABLE t1( c1 INT PRIMARY KEY, c2 INT, FOREIGN KEY (c2) REFERENCES t2(c2));Code language: SQL (Structured Query Language) (sql)

Make the values in c1 and c2 unique

CREATE TABLE t( c1 INT, c1 INT, UNIQUE(c2,c3));Code language: SQL (Structured Query Language) (sql)

Ensure c1 > 0 and values in c1 >= c2

CREATE TABLE t( c1 INT, c2 INT, CHECK(c1> 0 AND c1 >= c2));Code language: SQL (Structured Query Language) (sql)

Set values in c2 column not NULL

CREATE TABLE t( c1 INT PRIMARY KEY, c2 VARCHAR NOT NULL);Code language: SQL (Structured Query Language) (sql)

Modifying Data

Insert one row into a table

INSERT INTO t(column_list)VALUES(value_list);Code language: SQL (Structured Query Language) (sql)

Insert multiple rows into a table

INSERT INTO t(column_list)VALUES (value_list), (value_list), …;Code language: SQL (Structured Query Language) (sql)

Insert rows from t2 into t1

INSERT INTO t1(column_list)SELECT column_listFROM t2;Code language: SQL (Structured Query Language) (sql)

Update new value in the column c1 for all rows

UPDATE tSET c1 = new_value;Code language: SQL (Structured Query Language) (sql)

Update values in the column c1, c2 that match the condition

UPDATE tSET c1 = new_value, c2 = new_valueWHERE condition;Code language: SQL (Structured Query Language) (sql)

Delete all data in a table

DELETE FROM t;Code language: SQL (Structured Query Language) (sql)

Delete subset of rows in a table

DELETE FROM tWHERE condition;Code language: SQL (Structured Query Language) (sql)

Managing Views

Create a new view that consists of c1 and c2

CREATE VIEW v(c1,c2) ASSELECT c1, c2FROM t;Code language: SQL (Structured Query Language) (sql)

Create a new view with check option

CREATE VIEW v(c1,c2) ASSELECT c1, c2FROM t;WITH [CASCADED | LOCAL] CHECK OPTION;Code language: SQL (Structured Query Language) (sql)

Create a recursive view

CREATE RECURSIVE VIEW v ASselect-statement -- anchor partUNION [ALL]select-statement; -- recursive partCode language: SQL (Structured Query Language) (sql)

Create a temporary view

CREATE TEMPORARY VIEW v ASSELECT c1, c2FROM t;Code language: SQL (Structured Query Language) (sql)

Delete a view

DROP VIEW view_name;Code language: SQL (Structured Query Language) (sql)

Managing indexes

Create an index on c1 and c2 of the ttable

CREATE INDEX idx_name ON t(c1,c2);Code language: SQL (Structured Query Language) (sql)

Create a unique index on c3, c4 of the ttable

CREATE UNIQUE INDEX idx_nameON t(c3,c4)Code language: SQL (Structured Query Language) (sql)

Drop an index

DROP INDEX idx_name;Code language: SQL (Structured Query Language) (sql)

Managing triggers

Create or modify a trigger

CREATE OR MODIFY TRIGGER trigger_nameWHEN EVENTON table_name TRIGGER_TYPEEXECUTE stored_procedure;Code language: SQL (Structured Query Language) (sql)

WHEN

  • BEFORE – invoke before the event occurs
  • AFTER – invoke after the event occurs

EVENT

  • INSERT – invoke for INSERT
  • UPDATE – invoke for UPDATE
  • DELETE – invoke for DELETE

TRIGGER_TYPE

  • FOR EACH ROW
  • FOR EACH STATEMENT

Delete a specific trigger

DROP TRIGGER trigger_name;Code language: SQL (Structured Query Language) (sql)
SQL Cheat Sheet Download PDF it in PDF or PNG Format (2024)

FAQs

What is the data type for PDF file in SQL Server? ›

Storing and Displaying Files in a Database​

For example, in MySQL, the datatype that accepts PDF bytes is a LongBlob datatype, so you will need to set the PDF Data column to the LongBlob datatype. MS SQL accepts the Varbinary datatype, so you'll need to set the PDF Data column to a Varbinary datatype.

What is a SQL cheat sheet? ›

The SQL Basics Cheat Sheet provides you with the syntax of all basics clauses, shows you how to write different conditions, and has examples.

How long does it take to learn SQL? ›

Luckily, most can learn SQL in three to four weeks. This timeline may vary depending on whether you have prior programming experience, how you will use SQL, and other factors. We'll dive into those and everything else you should know about learning SQL below.

What is SQL used for? ›

Structured query language (SQL) is a programming language for storing and processing information in a relational database.

What data format is PDF? ›

PDF stands for "portable document format". Essentially, the format is used when you need to save files that cannot be modified but still need to be easily shared and printed. Today most devices open a PDF in an Internet Browser.

What are the data types in SQL PDF? ›

Character data types store string values and include CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, NCLOB, and LONG. Numeric types store numbers and include INTEGER, SMALLINT, BIGINT, REAL, DECIMAL, FLOAT, DOUBLE, and TINYINT. Date types store dates/times as DATE, TIME, and TIMESTAMP.

Can we learn SQL in 3 days? ›

By the end of the course, after 3 immersive days, SQL and python will be on your resume! This is the ultimate, immersive introduction to two of the most valuable skills today.

Is SQL better than Excel? ›

SQL uses multiple related tables that give it a multi dimensional feel. Excel can link multiple worksheets, but that's not its strength. Excel is a great program for simplicity and flexibility. SQL databases are excellent choices for storage, manipulation, and analysis of large amount of data.

Can you use SQL in Excel? ›

You may use Excel to get data from a big data source, such as an Access database, an SQL Server database, or even a huge text file. SQL statements in Excel allow you to connect to an external data source, parse fields or table contents, and import data without having to manually enter the data.

WHERE is SQL used in real life? ›

Almost every mid to large-sized organization uses SQL, including Facebook, Microsoft, LinkedIn, and Accenture. In fact, SQL was ranked as the most used database environment and the third most popular programming language by StackOverflow in 2021.

What is the data type of PDF? ›

PDF data types. The PDF document contains eight basic types of objects described below. These types are: booleans, numbers, strings, names, arrays, dictionaries, streams and the null object.

How to store PDF file in SQL Server? ›

How to upload PDFs in to SQL Server
  1. Enabling file stream feature in SQL Server. Go to SQL Server configuration manager and right click SQL Server service. Then we go to the filestream tab and enable file stream as shown below. ...
  2. Creating DEMO database. Create a new database with a name of your choice. ...
  3. Creating File Table.

How to read PDF file in SQL Server? ›

We used the following R functions:
  1. pdf_text to read a pdf file into a data frame.
  2. strsplit( x, "\n") to split the values inside our data frame by "\n" (Line Feed) character.
  3. substr( x, start, end) to extract substring value; our numbers.
  4. Unlist() to transform the list into a vector.
  5. Sprintf() to create our T-SQL syntax.

What is the data type of file in SQL Server? ›

Data types in SQL Server are organized into the following categories:
  • Exact numerics.
  • Approximate numerics.
  • Date and time.
  • Character strings.
  • Unicode character strings.
  • Binary strings.
  • Other data types.
May 21, 2024

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6222

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.