SQL SELECT statement

The SELECT statement is used to retrieve the data from database.

The SELECT is a DQL (Data Query Language) statement which is used for fetching the data from databases.

When and How to use SELECT statement?

SELECT statement is used, when you want to retrieve the data from single or multiple tables. To retrieve the data from single table used below query.

SELECT * FROM table_name;

Here, all the rows will get fetched from the table. The *(star) signifies all columns.

To retrieve the data from multiple tables, Join operation has to perform. There are mainly 4 types of joins in SQL. Click here to know More about SQL Joins

The below example shows you how to fetch data from multiple tables using Inner join.

SELECT * FROM table_name1 t1 INNER JOIN table_name2 t2
ON t1.column_name=t2.column_name;

SELECT statement examples

1) Select all the records from Student table?

Solution: The below query retrieves all the records from the student table. The *(star) signifies all columns.

SELECT * FROM Student;

2) Select all the records from Student table but only for 2 columns i.e. student name and student roll number?

Solution: The below query retrieves all the records from the student table but only for student name and student roll number columns. when you want to retrieve the data for limited columns then you should mention only those columns which are required. This makes query execution faster and it is good practice to follow.

SELECT student_name, student_roll_no FROM Student;

3) Select all the records from Student table for student name Joan?

Solution: The below query retrieves all the records from the student table but only for student name is Joan. You can use where clause to fetch the data for particular condition. If that condition satisfies then it shows you the data else it return 0 records retrieve message.

SELECT * FROM Student WHERE student_name='Joan';

Some interesting points of SELECT statement

Most of the SQL Query start with SELECT keyword.

The FROM is an SQL keyword. Mostly it is used in all SQL queries. After FROM there should be always table_name.

Previous Post Next Post

Contact Form