The select query is used to fetch records from a table, whenever you have the requirement to display or calculate some data from the table data, select query will be used.
For this query command, we will use the given table as an example.
id | first_name | last_name | |
1 | Ram | Singh | Ram89@gmail.com |
2 | Krishna | Singh | Krishna90@gmail.com |
Select all records with all columns (Properties)
There are two ways to write a query
- Without writing all the column names (using *)
- With writing all the column names
Without writing all column names (using *)
SELECT * FROM users;
id | first_name | last_name | |
1 | Ram | Singh | Ram89@gmail.com |
2 | Krishna | Singh | Krishna90@gmail.com |
With writing all column names
SELECT id, first_name, last_name, email FROM users;
id | first_name | last_name | |
1 | Ram | Singh | Ram89@gmail.com |
2 | Krishna | Singh | Krishna90@gmail.com |
Fetch all records with specific columns
Select id, first_name, email FROM users;
id | first_name | |
1 | Ram | Ram89@gmail.com |
2 | Krishna | Krishna90@gmail.com |
Fetch specific records (WHERE clause)
WHERE clause is used to filter records from the table, or it is used whenever we want to fetch records from the table with the conditions.
SELECT * FROM users WHERE id < 2;
In this query command, we are fetching only those records which have an id value less than 2.
id | first_name | last_name | |
1 | Ram | Singh | Ram89@gmail.com |