SQL Select

Selecting Data from Table

In the previous chapter, we learned how to insert data into a database table. Now it's time to select data from existing tables using SQL queries.

The SELECT statement is used to retrieve data from one or more tables. This statement can be used to fetch all the rows from a table at once or to retrieve only those rows that meet specific conditions or a combination of conditions.

Syntax

The basic syntax for selecting data from a table is:

SELECT column1_name, column2_name, columnN_name FROM table_name;

Here, column1_name, column2_name, ... are the names of the columns or fields in the table whose values you want to fetch. If you want to fetch values from all columns in a table, you can use the following syntax:

SELECT * FROM table_name;

Let's apply these statements practically. Suppose we have 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 Records from Table

This SQL query retrieves all rows from the employees table:

SELECT * FROM employees;

After running the query, the result will resemble the following:

+--------+--------------+------------+--------+---------+
| 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 observed, it displays all rows and columns from the employees table.

Tip: The asterisk (*) acts as a wildcard character representing all columns. In the example above, it retrieves all columns from the employees table.

Select Specific Columns from Table

To fetch specific columns only, use the following syntax:

SELECT emp_id, emp_name, hire_date, salary
FROM employees;

Upon executing the aforementioned statement, the output will resemble the following:

+--------+--------------+------------+--------+
| 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 |
+--------+--------------+------------+--------+

Noticeably, this time the result set excludes the dept_id column. In the forthcoming chapter, we will delve into selecting records from a table based on specific conditions.