SQL SELECT Query | Retrieve Data From Database

The SQL SELECT Query retrieves or fetches data from the database. We can fetch an entire table or a set of columns using the query. It stores the data in a result table. In this article, we will discuss the SQL SELECT Query. Also, we will discuss a few examples of using it.

Syntax

SELECT column1, column2 … columnN FROM tableName;

We can provide a list of columns in the query to fetch the values in those columns. Also, we can fetch all the columns of a table using *.

SELECT * from table_name;

The query with * selects all the columns from the table. Also, it is mandatory to pass atleast one column in the SELECT Query.

Demo Table

For instance, consider the demo table to refer to all the queries in this article.

+----+--------+--------+------------+-------+
| id | name   | salary | experience | team  |
+----+--------+--------+------------+-------+
|  1 | David  |  20000 |          5 | Alpha |
|  2 | Monica |  25000 |          6 | Beta  |
|  3 | John   |  15000 |          3 | Beta  |
|  4 | Mary   |  18000 |          4 | Gamma |
|  5 | Ross   |  30000 |          8 | Alpha |
+----+--------+--------+------------+-------+

SELECT All Columns From Table

We can select all the columns from the table using * in the column field in the SQL SELECT Query.

SELECT * FROM employees;

This query will fetch all the columns and rows from the table.

+----+--------+--------+------------+-------+
| id | name   | salary | experience | team  |
+----+--------+--------+------------+-------+
|  1 | David  |  20000 |          5 | Alpha |
|  2 | Monica |  25000 |          6 | Beta  |
|  3 | John   |  15000 |          3 | Beta  |
|  4 | Mary   |  18000 |          4 | Gamma |
|  5 | Ross   |  30000 |          8 | Alpha |
+----+--------+--------+------------+-------+

SELECT Some Columns From Table

Similarly, we can only fetch a few columns from the table by passing the list of columns in the SQL SELECT Query.

SELECT name, experience FROM employees;

The above query will only fetch the name and experience column from the table as follows:

+--------+------------+
| name   | experience |
+--------+------------+
| David  |          5 |
| Monica |          6 |
| John   |          3 |
| Mary   |          4 |
| Ross   |          8 |
+--------+------------+

Conclusion

In this article, we discussed the SQL SELECT Query. You can read more about it on Wikipedia. Additionally, you can also read more articles on SQL Tutorial on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *