Skip to main content

Using the SELECT Statement

Introduction

SQL, or Structured Query Language, is a language designed to manage data held in a relational database management system (RDBMS). One of the most commonly used SQL commands is SELECT which is used to query the data in a database. In this tutorial, we will focus on understanding the SELECT statement in SQL.

What is a SELECT Statement?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, also known as the result-set.

Syntax:

SELECT column1, column2,...
FROM table_name;

Here, column1, column2 are the field names of the table you want to select data from. If you want to select all the fields available in the field, use the following syntax:

SELECT * FROM table_name;

Examples

Let's consider an example where we have a table called Students with the following data:

StudentIDFirstNameLastNameAge
1JohnDoe20
2JaneDoe22
3JimBeam23
4JackDaniels24

Now, if we want to select only the FirstName from the Students table, we would use the SELECT statement as follows:

SELECT FirstName FROM Students;

This will return:

FirstName
John
Jane
Jim
Jack

If you want to select all data from the Students table, use the following SELECT statement:

SELECT * FROM Students;

This will return all the data from the Students table.

The DISTINCT Keyword

In a database, there may be duplicate data. If you want to select all unique items from a column, you use the DISTINCT keyword.

Syntax:

SELECT DISTINCT column_name FROM table_name;

For example, if we have a Courses table with the following data:

CourseIDCourseName
1Math
2English
3Math
4Science

We can select the unique courses as follows:

SELECT DISTINCT CourseName FROM Courses;

This will return:

CourseName
Math
English
Science

Conclusion

The SELECT statement is a crucial part of SQL as it allows us to retrieve data from a database. Understanding how to use the SELECT statement effectively can greatly aid in managing and manipulating data within a database.

Remember that practice is key when it comes to learning SQL. Try creating your own database and use the SELECT statement to retrieve data. In the next tutorial, we will cover more advanced topics in SQL. Happy learning!