Beginners Guide to SQL – SQL SELECT Statement

Hits: 6

(SQL Tutorials for Citizen Data Scientist)

SQL SELECT Statement

In this tutorial you will learn how to select records from database tables using SQL.

Selecting Data from Table

In the previous chapter we’ve learned how to insert data in a database table. Now it’s time to select the data from existing tables using the SQL query.

The SELECT statement is used to select or retrieve the data from one or more tables. You can use this statement to retrieve all the rows from a table in one go, as well as to retrieve only those rows that satisfy a certain condition or a combination of conditions.

Syntax

The basic syntax for selecting the data from a table can be given with:

SELECT column1_namecolumn2_namecolumnN_name FROM table_name;

Here, column1_namecolumn2_name, … are the names of the columns or fields of a database table whose values you want to fetch. However, if you want to fetch the values of all the columns available in a table, you can just use the following syntax:

SELECT * FROM table_name;

Let’s put these statements into real use. Suppose we’ve a table named employees in our database that contains the following records:

+--------+--------------+------------+--------+---------+
| emp_id | emp_name     | hire_date  | salary | dept_id |
+--------+--------------+------------+--------+---------+
|      1 | Ethan Hunt   | 2001-05-01 |   5000 |       4 |
|      2 | Tony Montana | 2002-07-15 |   6500 |       1 |
|      3 | Sarah Connor | 2005-10-18 |   8000 |       5 |
|      4 | Rick Deckard | 2007-01-03 |   7200 |       3 |
|      5 | Martin Blank | 2008-06-24 |   5600 |    NULL |
+--------+--------------+------------+--------+---------+

Select All from Table

The following statement will return all the rows from the employees table.

Example

SELECT * FROM employees;

After execution, the output will look something like this:

+--------+--------------+------------+--------+---------+
| emp_id | emp_name     | hire_date  | salary | dept_id |
+--------+--------------+------------+--------+---------+
|      1 | Ethan Hunt   | 2001-05-01 |   5000 |       4 |
|      2 | Tony Montana | 2002-07-15 |   6500 |       1 |
|      3 | Sarah Connor | 2005-10-18 |   8000 |       5 |
|      4 | Rick Deckard | 2007-01-03 |   7200 |       3 |
|      5 | Martin Blank | 2008-06-24 |   5600 |    NULL |
+--------+--------------+------------+--------+---------+

As you can see, it returns all the rows and columns from the employees table.

Tip: The asterisk (*) is a wildcard character that means everything. For example, the asterisk character in the SELECT statement of the example above is a shorthand substitute for all the columns of the employees table.

Select Columns from Table

If you don’t require all the data, you can select specific columns, like this:

Example

SELECT emp_id, emp_name, hire_date, salary
FROM employees;

After executing the above statement, you’ll get the output something like this:

+--------+--------------+------------+--------+
| emp_id | emp_name     | hire_date  | salary |
+--------+--------------+------------+--------+
|      1 | Ethan Hunt   | 1995-10-30 |   5000 |
|      2 | Tony Montana | 1990-07-15 |   6500 |
|      3 | Sarah Connor | 2011-04-13 |   5600 |
|      4 | Rick Deckard | 2005-10-18 |   7200 |
|      5 | Martin Blank | 1996-05-24 |   8000 |
+--------+--------------+------------+--------+

As you can see this time there is no dept_id column in the result set. In the next chapter we’ll learn how to select the records from a table based on a condition.

 

Beginners Guide to SQL – SQL SELECT Statement

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!