SQL Command Cheat Sheet

image_print

Usage: Retrieves data from a database.

SELECT column1, column2, ... FROM table_name;

Usage: Filters records that meet certain conditions.

SELECT column1, column2, ... FROM table_name WHERE condition;

Usage: Sorts the results returned by a SQL query.

SELECT column1 FROM table_name ORDER BY column1 ASC;

Usage: Specifies the maximum number of records to return.

SELECT column1 FROM table_name LIMIT 10;

Usage: Combines rows from two or more tables based on a related column.

SELECT a.column1, b.column2 FROM table1 a INNER JOIN table2 b ON a.common = b.common;

Usage: Returns all records from the left table, and the matched records from the right table, or NULL for no match.

SELECT a.column1, b.column2 FROM table1 a LEFT JOIN table2 b ON a.common = b.common;

Usage: Returns all records from the right table, and the matched records from the left table, or NULL for no match.

SELECT a.column1, b.column2 FROM table1 a RIGHT JOIN table2 b ON a.common = b.common;

Usage: Combines the results of both LEFT and RIGHT joins.

SELECT a.column1, b.column2 FROM table1 a FULL OUTER JOIN table2 b ON a.common = b.common;

Usage: Returns the Cartesian product of the sets of records from the two joined tables.

SELECT a.column1, b.column2 FROM table1 a CROSS JOIN table2 b;

Usage: Uniquely identifies each row in a table.

ALTER TABLE table_name ADD CONSTRAINT pk_name PRIMARY KEY (column1);

Usage: Ensures the referential integrity of the data in one table to match values in another table.

ALTER TABLE child_table ADD CONSTRAINT fk_name FOREIGN KEY (column1) REFERENCES parent_table (column1);

Usage: Ensures that all values in a column satisfy a specific condition.

ALTER TABLE table_name ADD CONSTRAINT ck_name CHECK (condition);

Usage: Groups rows that have the same values in specified columns into summary rows.

SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

Usage: Deletes rows from a table based on a condition.

DELETE FROM table_name WHERE condition;

Usage: Deletes all rows in a table without logging the individual row deletions.

TRUNCATE TABLE table_name;

Usage: Modifies the structure of an existing table, such as adding a column or changing a column’s type.

ALTER TABLE table_name ADD column_name datatype;

Usage: Concatenates two or more strings into one string.

SELECT CONCAT(column1, ' ', column2) AS full_name FROM table_name;
image_print

Share on :

Social Share

Recent Post

© 2024 All rights reserved by Go1digital.com