SQL For Beginners - SELECT Statement

SQL is a language of commands and one of the most commonly used SQL command is SELECT. SELECT is a DQL Command, that is Data Query Language Command. I have given a brief description of the various types of SQL Commands in "SQL For Beginners - Introduction" which was the first article of the series SQL For Beginners.

Now, we are going to learn about the SELECT Statement in detail. SELECT Statement is used to retrieve data from the table.

Syntax:

To select all the columns:

SELECT * FROM Table_Name;

To select specific columns:

SELECT column1, column2, columnN FROM Table_Name;

Here,
  • SELECT is the keyword used to retrieve data from the table.

    * means all the columns.

  • FROM is the keyword which comes before the Table_Name;
Let us now see the usage of SELECT Statement practically.

I am using Microsoft SQL SERVER 2012 for practicals. You can download it from Microsoft's website. Pre-release version of SQL SERVER 2014 is also available and if interested, one can install it by referring to the article "Installing SQL Server 2014" which guides you step by step in installing SQL SERVER 2014.

Suppose, I have a "Students" table which contains "StudentID" and "StudentName" Column (Attributes). The table has details of 5 students and we want to view all the data of the table. Then, we will use the Select Statement as:
  1. SELECT * FROM Students;  
Output:



As you can see in the output, the Select Statement retrieves all the 5 rows from the table Students. Now, suppose if we need to just fetch the Names of the Students, then our SELECT statement will be:
  1. SELECT StudentName from Students;  
Output:


Here, as you can see only the StudentName column is displayed.

So, till now we have seen that SELECT Statement is used to retrieve either all the columns or specific-columns from the table. But, there will be many situations where we do not want to retrieve all the rows. We need a specific or few rows from the table. Then, in such scenarios, we use the WHERE Clause. The WHERE Clause allows us to retrieve only the required records.

Suppose, we know the StudentID of a particular student and we want to get his/her name then in such situation we will use WHERE Clause along with the SELECT Statement.

Select StudentName from Students where StudentID = 1

This query will give us the name of the Student whose ID is 1, that is Raj. 

We will learn about the WHERE Clause and Expressions in detail in the next article. Let me know if you have any doubts or if you have not understood anything. 

Up Next
    Ebook Download
    View all
    Learn
    View all