DEV Community

Cover image for SQL Beginner Friendly Cheatsheet
Durgesh Mahajan
Durgesh Mahajan

Posted on

SQL Beginner Friendly Cheatsheet

1. Retrieving/Selecting Data -

The SELECT statement is used to select data from a database.

SELECT column_name FROM table_name;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT StudentName FROM Student;
Enter fullscreen mode Exit fullscreen mode


2. Selecting All Records/Columns of Data -

The Asterisk(*) is used to select all records.

SELECT * FROM table_name;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT * FROM Student;
Enter fullscreen mode Exit fullscreen mode


3. Selecting Distinct Data -

The SELECT DISTINCT statement is used to return only distinct (different) values.

SELECT DISTINCT column_name FROM table_name;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT DISTINCT StudentName FROM Student;
Enter fullscreen mode Exit fullscreen mode


4. Filtering the Records -

The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.

SELECT column_name FROM table_name WHERE condition;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT * FROM Student WHERE Marks>=450;
Enter fullscreen mode Exit fullscreen mode


5. Filtering the Records by two or more conditions -

The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition.
The NOT operator displays a record if the condition(s) is NOT TRUE.

SELECT column_name FROM table_name WHERE condition1 AND condition2 OR condition3;
Enter fullscreen mode Exit fullscreen mode
SELECT column_name FROM table_name WHERE NOT condition;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT * FROM Student WHERE Marks>=450 AND Div="A";
Enter fullscreen mode Exit fullscreen mode
SELECT * FROM Student WHERE NOT Marks=500;
Enter fullscreen mode Exit fullscreen mode


6. Sorting the Data of Records -

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

SELECT column_name FROM table_name ORDER BY column_name;
Enter fullscreen mode Exit fullscreen mode

for eg -

SELECT * FROM Student ORDER BY Marks DESC;
Enter fullscreen mode Exit fullscreen mode


7. Inserting Data -

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Enter fullscreen mode Exit fullscreen mode

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table.

for eg -

INSERT INTO Student (StudentName, Class, Div, Marks)
VALUES ("Durgesh", "Second Year", "B", 450);
Enter fullscreen mode Exit fullscreen mode


8. Updating Data -

The UPDATE statement is used to modify the existing records in a table.
If you omit the WHERE clause, ALL records will be updated!

UPDATE table_name SET column=value WHERE condition;
Enter fullscreen mode Exit fullscreen mode

for eg -

UPDATE Student SET Marks=440 WHERE StudentName="Durgesh";
Enter fullscreen mode Exit fullscreen mode


Follow for more :-)

Linkedin
Github
Instagram

Top comments (0)