SQL For Beginners - SELECT Statement

Introduction

SQL is a language of commands, and one of the most commonly used SQL commands is SELECT. SELECT is a DQL Command, that is, Data Query Language Command. I have briefly described the various types of SQL Commands in "SQL For Beginners - Introduction," 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 that comes before the Table_Name;

Let us now see the usage of the SELECT Statement practically.

I am using Microsoft SQL SERVER 2012 for practicals. You can download it from Microsoft's website. The pre-release version of SQL SERVER 2014 is also available. If interested, one can install it by referring to the article " style=" color:#0000ff">Installing SQL Server 2014," which guides you step by step in installing SQL SERVER 2014.

Suppose you have a "Students" table that contains the "StudentID" and "StudentName" Columns (Attributes). The table details five students, and we want to view all the dtable's data. Then, we will use the Select Statement as.

SELECT * FROM Students;  

Output

As you can see in the output, the Select Statement retrieves all five rows from the table Students. Now, suppose we need to fetch the Students' names; then SELECT Statement will be.

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 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 clear 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 want to get their name. Then in such a 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, Raj. 

Conclusion

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

Recommended Ebook

SQL Queries For Beginners

Download Now!
Similar Articles