1. Retrieving/Selecting Data -
The SELECT
statement is used to select data from a database.
SELECT column_name FROM table_name;
for eg -
SELECT StudentName FROM Student;
2. Selecting All Records/Columns of Data -
The Asterisk(*
) is used to select all records.
SELECT * FROM table_name;
for eg -
SELECT * FROM Student;
3. Selecting Distinct Data -
The SELECT DISTINCT
statement is used to return only distinct (different) values.
SELECT DISTINCT column_name FROM table_name;
for eg -
SELECT DISTINCT StudentName FROM Student;
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;
for eg -
SELECT * FROM Student WHERE Marks>=450;
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;
SELECT column_name FROM table_name WHERE NOT condition;
for eg -
SELECT * FROM Student WHERE Marks>=450 AND Div="A";
SELECT * FROM Student WHERE NOT Marks=500;
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;
for eg -
SELECT * FROM Student ORDER BY Marks DESC;
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, ...);
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);
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;
for eg -
UPDATE Student SET Marks=440 WHERE StudentName="Durgesh";
Top comments (0)