Select statement is used to fetch data from relational database. A relational database is organized collection of data. As we know that data is stored inside tables in a database. SQL select statement or SQL select query is used to fetch data from one or more than one tables. We can fetch few columns, few rows or entire table using SELECT Query based on the requirement.
SELECT Syntax
One column:
Here column_name is the name of the column for which we need to fetch data and table_name is the name of the table in the database.
SELECT column_name FROM table_name;
More than one columns:
SELECT column_name_1, column_name_2, ... FROM table_name;
For example – Lets say we want to fetch column_a and column_x of table named “ABC”. The query for this should be:
SELECT column_a, column_x from ABC;
For fetching entire table:
SELECT * FROM table_name;
Select Statement Example – Retrieve single column
Lets say we have a table EMPLOYEES
with the following data:
+------+----------+---------+----------+ | SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 6588.00 | | 589 | Apoorv | 21 | 1400.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
Lets say we want to fetch a single column EMP_NAME from the given table – we can write a SQL statement select like this:
SELECT EMP_NAME FROM EMPLOYEES;
Output of the above Query:
+----------+ | EMP_NAME | +------+---+ | Steve | | Peter | | Shubham | | Chaitanya| | Apoorv | | Rajat | | Ajeet | +----------+
SQL Select Example – Fetching multiple columns
Lets say we want to fetch multiple columns SSN
& EMP_NAME
from the above table EMPLOYEES
, we can write the SELECT Query like this:
SELECT SSN, EMP_NAME FROM EMPLOYEES;
Output of the above query:
The Query would produce the following result.
+------+----------+ | SSN | EMP_NAME | +------+----------+ | 101 | Steve | | 223 | Peter | | 388 | Shubham | | 499 | Chaitanya| | 589 | Apoorv | | 689 | Rajat | | 700 | Ajeet | +------+----------+
SELECT * Example – Fetching complete table data
To fetch the entire EMPLOYEES
table:
SELECT * FROM EMPLOYEES;
Result:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 6588.00 | | 589 | Apoorv | 21 | 1400.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
SELECT statement Clause
In the above examples, we have seen how to fetch single column, multiple columns and entire table using SELECT statement. What if we want to fetch only few rows or want the rows in an particular order or grouped rows etc.? We can do so with the help of clauses in SQL select statement.
These clauses are optional but they are frequently used in SQL statements in the real world database operations.
Clauses are:
Note: We have separate tutorials for each of these clauses so you do not have to worry about them now, this information is provided here to let you know that these clauses exist and frequently used with SQL select statements.
WHERE Clause in SQL: WHERE clause filters the records, with the help of this clause we specify a condition in the SQL statement and only those records that fulfil the condition are retrieved from database.
ORDER BY Clause in SQL: ORDER BY clause is used to return the rows in ascending or descending order of the data.
GROUP BY Clause in SQL: GROUP BY clause groups the rows with same data, this is often used along with the aggregate functions.
HAVING Clause in SQL: HAVING clause filters the records, just like WHERE clause, however it is used along with the GROUP BY clause so it filters the records from the output set of rows produced by GROUP BY clause.
Leave a Reply