SQL SELECT Statement is explained with definition, syntax, query, & example. It is also given with different conditions like WHERE, IN, LIMIT, ORDER BY, GROUP BY, HAVING. Once, you read complete this tutorial, you definitely select data from specified/all columns of a table.
SQL SELECT Statement
Definition – SQL SELECT statement retrieves existing data from specified columns of a table.
SELECT Syntax
You can select data from a table with the help of the following syntax
Syntax –
SELECT column1_name, column2_name, column3_name,... FROM table_name;
Important Points –
- You can write a SELECT statement in Uppercase or lowercase.
- Column Names must be separated by a comma
- Before executing the SELECT query, you must create a SQL database & table
- Always declare column names without quotation.
- You can execute the SELECT query multiple times.
- You can skip the column name from the SELECT query if you need to select values from all column of a table
SELECT Example
Now, You will earn to execute the SELECT statement with the following table. This table is created for showing an example. once you understand it. you can easily select data from your own table of the project.
Table Name – students
This table will be joined to subjects
table based on a common column subject_id
.
Table Name – students
student_id | student_name | gender | city |
411 | Navneet Singh | Male | Panjab |
412 | Mr. Tommy | Male | Jaipur |
413 | Mrs. Monika | Female | Patna |
414 | Mrs. Maliska | Female | Patna |
415 | Ravish Kumar | Male | New Delhi |
416 | Sanjay Agarwal | Male | Mumbai |
417 | Babita Mishra | Female | New Delhi |
418 | Niel Patel | Male | Noida |
419 | Pushpa Yadav | Female | Patna |
420 | Aryan Singh | Male | Noida |
421 | Javed Hassan | Male | Jaipur |
SQL SELECT Data From All Columns
If you need to select data from all columns of table, you can use strict * symbole instead of column name.
Syntax –
SELECT * FROM table_name;
Example –
You can select data from all column of students
table using the follwing query
Query –
SELECT * FROM students
SQL SELECT Data FROM Specified Column
You can also select data from specified column of table. Let’s understand it through the following example
Example – 1
If you want to select only student_name
from students
table then use the follwing query
Query –
SELECT student_name FROM students;
Example – 2
If you want to select only student_name
& city
from students
table then use the following query
Query –
SELECT student_name, city FROM students;
SQL SELECT Conditions
Now, Let’s go to learn the SELECT statement with the following different conditions.
SELECT WHERE
You can use SELECT statement with WHERE clause if you have to retrieve data on a specified condition
Syntax –
SELECT column1_name, column2_name,... FROM table_name WHERE condition;
Note – You must declare WHERE condition after the table name.
Example –
If you have to select only a male student name then you will have to use the WHERE condition in the SELECT query
Query –
SELECT student_name FROM students WHERE gender='Female';
Output –
student_name |
Mrs. Monika |
Mrs. Maliska |
Babita Mishra |
Pushpa Yadav |
SELECT IN
You can use SELECT statement with IN operator if you have to retrieve data based on multiple conditions
Syntax –
SELECT column1_name, column2_name,... FROM table_name IN (column_value1, column_value2,...);
Note – You must declare IN operator after the table name.
Example –
If you have to select cities and student names who belongs to Jaipur or Noida then you will have to use IN operator in the SELECT query
Query –
SELECT student_name, city FROM students IN ('Jaipur', Noida)
Output –
student_name | city |
Mr. Tommy | Jaipur |
Javed Hassan | Jaipur |
Neil Patel | Noida |
Aryan Singh | Noida |
SELECT LIMIT
You can use a SELECT statement with the LIMIT condition if you have to retrieve data with only a limited number of records.
Syntax –
SELECT column1_name, column2_name,... FROM table_name LIMIT number;
Note – You must declare LIMIT after the table name.
Example –
If you have only 4 student names then you will have to use LIMIT in the SELECT query
Query –
SELECT student_name FROM students LIMIT 4;
Output –
student_name |
Navneet Singh |
Mr. Tommy |
Mrs. Monika |
Mrs. Maliska |
SELECT ORDER BY
You can use a SELECT statement with an ORDER BY clause if you have to retrieve data in descending or ascending order.
Syntax –
SELECT column1_name, column2_name,... FROM table_name ORDER BY column_name DESC|ASC;
Note – You must declare the ORDER BY clause after the table name.
Example –
If you have to select only student names in descending order then you will have to use ORDER BY clause in SELECT query.
Query –
SELECT student_name FROM students ORDER BY student_name DESC;
Output –
student_name |
Sanjay Agarwal |
Ravish Kumar |
Pushpa Yadav |
Navneet Singh |
Niel Patel |
Mr. Tommy |
Mrs. Monika |
Mrs. Maliska |
Javed Hassan |
Babita Mishra |
Aryan Singh |
SELECT GROUP BY
You can use a SELECT statement with a GROUP BY clause if you have to retrieve data based on a group of the same values.
Syntax –
SELECT column1_name, column2_name,... FROM table_name GROUP BY column1_name, column2_name,...
Note – You must declare the GROUP BY clause after the table name.
Example –
If you have to select the city and the total number of students from a particular city then you will have to use the GROUP BY clause in the SELECT query.
Query –
SELECT city, count(student_name) FROM students GROUP BY city;
Output –
city | count(student_name) |
Panjab | 1 |
Jaipur | 2 |
Patna | 3 |
New Delhi | 2 |
Mumbai | 1 |
Noida | 2 |
SELECT HAVING
You can use a SELECT statement with a HAVING BY clause if you have to retrieve data based on groups of the same values.
Syntax –
SELECT column1_name, column2_name,... FROM table_name HAVING condition
Note – You must declare the HAVING clause after the table name.
Example –
If you need to know the names of the city that has less than 2 students.
Query –
SELECT city, count(student_name) FROM students GROUP BY city; HAVING count(student_name)<2;
Output –
city | count(student_name) |
Panjab | 1 |
Mumbai | 1 |
How to Execute SQL SELECT Statement
you have to run the following steps to execute SQL SELECT Statement–
- First of all, Install MySQL Server Package
- Create a MySQL Database like
mydb
- Now, Create a table like
students
- Insert data into the created table
- Write a SELECT query command to retrieve data from a table.
mysql>CREATE DATABASE mydb; mysql>USE mydb; mysql>CREATE TABLE students ( student_name varchar(50), gender varchar(20), city varchar(100) ); mysql>INSERT INTO students ( student_name, gender, city) VALUES ( 'Navneet Singh', 'Male', 'Panjab'), ('Mr Tommy', 'Male', 'Jaipur'), ( 'Mrs Monika', 'Female', 'Patna'), ('Mrs Maliska', 'Female', 'Patna'), ( 'Ravish Kumar', 'Male', 'New Delhi'), ('Sanjay Agarwal', 'Male', 'Mumbai'), ('Babita Mishra', 'Female', 'New Delhi'), ('Neil Patel', 'Male', 'Noida'), ('Pushpa Yadav', 'Female', 'Patna'), ('Aryan Singh', 'Male', 'Noida'), ('Javed Hassan', 'Male', 'Jaipur'); mysql>SELECT student_name, city FROM students;
Tutorial Suggestion –
You have learned to select values from a table using the SELECT statement. Now you can easily implement it with different & complex queries.
If you need to ask questions related to SQL, Kindly ask me through the comment box. Even you should share it with your friends.