- 5 years ago
- Zaid Bin Khalid
- 2,475 Views
-
4
In this tutorial section, we will learn and create on how to Select records from database tables by using SQL.
The SELECT STATEMENT basically used to select the data from one or more tables.
Selecting Data from Table.
In the previous tutorials, We had learned and create “How to insert data” in a database table. Now In this session, we will talk about selecting the data with the help of SELECT STATEMENT from the tables.
Syntax.
The basic syntax for selecting the data from a table can be given with.
SELECT column1_name, column2_name, columnN_name FROM table_name;
Here, column1_name, column2_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;
Now we insert these statements into our real use. Let suppose we have a table named employees in our database that have mention below the following record.
+--------+--------------+------------+--------+---------+
| 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.
Following statement will return all the row values within employees table.
After executing the Statement, Its show the resulting output 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 |
+--------+--------------+------------+--------+---------+
Select Columns from Table.
In case you don’t require all the data from the table, you can select specific columns as mention below in the example.
SELECT emp_id, emp_name, hire_date, salary
FROM employees;
After executing the Statement, Its show the resulting output 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 |
+--------+--------------+------------+--------+
It can be noticed that there is no dept_id column displayed in the output result set.
- 5 years ago
- Zaid Bin Khalid
- 2,475 Views
-
4