DEV Community

Cover image for SQL Cheat Sheet #2
GharamElhendy
GharamElhendy

Posted on

SQL Cheat Sheet #2

Here are some SQL commands to jog your memory!

Return the number of records in a table

SELECT COUNT(*) FROM table_name
Enter fullscreen mode Exit fullscreen mode

Show the entire table

SELECT * FROM table_name
Enter fullscreen mode Exit fullscreen mode

Show a column within the table

SELECT column_name FROM table_name
Enter fullscreen mode Exit fullscreen mode

Select only the unique values within a column

SELECT DISTINCT column_name FROM table_name
Enter fullscreen mode Exit fullscreen mode

Return the number of unique values from a column within a table

SELECT COUNT(DISTINCT column_name)
FROM table_name
Enter fullscreen mode Exit fullscreen mode

Select all records where a certain value exists

SELECT * FROM table_name 
WHERE column_name = 'value'
Enter fullscreen mode Exit fullscreen mode

Select all records excluding a certain value

SELECT * FROM table_name WHERE NOT column_name = 'value'
Enter fullscreen mode Exit fullscreen mode

Select all records where a certain value exists in a column and another certain value exists in another column

SELECT * FROM table_name 
WHERE column1 = 'string1' AND column_2 = 'string2'
Enter fullscreen mode Exit fullscreen mode

Select all records that start with a particular letter from the alphabet (in this case, the letter = a)

SELECT * FROM table_name WHERE column_name LIKE 'a%'
Enter fullscreen mode Exit fullscreen mode

Select all records that end with a particular letter from the alphabet (in this case, the letter = a)

SELECT * FROM table_name WHERE column_name LIKE '%a'
Enter fullscreen mode Exit fullscreen mode

Select all records with values from a column alphabetically ranging from a certain value to another certain value

SELECT * FROM table_name BETWEEN 'string1' AND 'string2'
Enter fullscreen mode Exit fullscreen mode

Return records in a descending order

SELECT * FROM table_bame ORDER BY column_name DESC
Enter fullscreen mode Exit fullscreen mode

Select records from a column with multiple values

SELECT * FROM Customers
WHERE column_name IN ('value1','value2');
Enter fullscreen mode Exit fullscreen mode

Insert records in your table

INSERT INTO table_name VALUES ('first','second,'third','nth')
Enter fullscreen mode Exit fullscreen mode

It's worth noting that the number of values you insert after the "INSERT INTO VALUES" has to correspond to the number of columns in the table you're inserting information into.

Change a record in a column to another value

UPDATE table_name SET column_name = 'new value' WHERE column_name = 'old value'
Enter fullscreen mode Exit fullscreen mode

Remove records where a certain value exists

DELETE FROM table_name WHERE column_name = 'certain value'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)