BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

SQL SELECT Statement

Last Updated: November 27, 2018 by Chaitanya Singh | Filed Under: SQL

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.

❮ PreviousNext ❯

Top Related Articles:

  1. Introduction to SQL
  2. SQL – DROP Table Statement to delete the entire table
  3. DELETE Query in SQL
  4. SQL SELECT COUNT
  5. SQL SELECT AVG() Function

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

SQL Tutorial

  • SQL Tutorial
  • SQL Introduction
  • SQL Syntax
  • SQL Data Types

SQL Database

  • SQL CREATE DB
  • SQL DROP DB
  • SQL Rename DB
  • SQL USE DB

SQL Queries

  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap