SQL SELECT Statement
Introduction
SQL SELECT statement is most commonly used to query the database to retrieve the selected data, according to the necessary condition from the stored table.
Syntax:
SELECT column_name
FROM table_name
WHERE condition;
Example:
Consider the following table titled
'Students' that contains information of the students.
Student_ID | LastName | FirstName | Marks |
---|
1 | Patil | Ravi | 60 |
2 | Morya | Surendra | 60 |
3 | Singh | Jaya | 78 |
1. To display all data from the table 'Students', the query should be written as:
SELECT * FROM Students;
2. To select LastName of all the students, the query should be:
SELECT LastName FROM Students;
The result is shown in the following table.
3. To obtain LastName from table 'Students' for the students securing 60 marks, the query should be written as:
SELECT LastName FROM Students
WHERE Marks = 60;
The result is shown in the following table.
SQL SELECT DISTINCT
SQL SELECT DISTINCT statement is used to find only unique records from the table.
Syntax:
SELECT DISTINCT column_name1, column_name2, ….......
FROM table_name;
Example:
Consider the following table
'Students'.
Student_ID | LastName | FirstName | Marks |
---|
1 | Patil | Ravi | 60 |
2 | Morya | Surendra | 60 |
3 | Singh | Jaya | 78 |
1. If the user needs unique records for column 'Marks', the query should be written as:
SELECT DISTINCT Marks FROM student;
The result is shown in the following table.