Details on SQL Select Statement

SELECT query is used to retrieve data from a table. It is the most used SQL query. We can retrieve complete table data, or partial by specifying conditions using the WHERE clause.

Syntax of SELECT query

SELECT query is used to retieve records from a table. We can specify the names of the columns which we want in the resultset.

    FROM table_name;

Time for an Example

Consider the following student table,

s_id name age address
101 Adam 15 California
102 Alex 18 Delhi
103 Ada 17 Awka
104 Chidi 22 Udi
SELECT s_id, name, age FROM student;

The above query will fetch information of s_idname and age columns of the student table and display them,

s_id name age
101 Adam 15
102 Alex 18
103 Ada 17
104 Chidi 22

As you can see the data from address column is absent, because we did not specif it in our SELECT query.

Select all records from a table

A special character asterisk * is used to address all the data(belonging to all columns) in a query. SELECT statement uses * character to retrieve all records from a table, for all the columns.

SELECT * FROM student;

The above query will show all the records of student table, that means it will show complete dataset of the table.

s_id name age address
101 Adam 15 California
102 Alex 18 Delhi
103 Ada 17 Awka
104 Chidi 22 Udi

Select a particular record based on a condition

We can use the WHERE clause to set a condition,

SELECT * FROM student WHERE name = 'Ada';

The above query will return the following result,

103 Ada 17 Awka

Performing Simple Calculations using SELECT Query

Consider the following employee table.

eid name age salary
101 Adam 26 5000
102 Ricky 42 8000
103 Ada 25 10000
104 Chidi 22 5000

Here is our SELECT query,

SELECT eid, name, salary+3000  FROM employee;

The above command will display a new column in the result, with 3000 added into existing salaries of the employees.

eid name salary+3000
101 Adam 8000
102 Ricky 11000
103 Ada 13000
104 Rohan 8000

So you can also perform simple mathematical operations on the data too using the SELECT query to fetch data from table.