SELECT statement
Overview
The SELECT
statement helps you obtain the data you need from one or more tables.
The application of this statement will be helpful in several cases listed below:
-
Evaluating data from only particular fields in a table.
-
Reviewing data from several tables at the same time.
-
Retrieving the data based on specific criteria.
Syntax
To request data from a table using the SELECT
statement, you can use the following syntax:
You are allowed to filter the table by column. Refer to the syntax below.
We will define each syntax as follows.
-
SELECT
determines the data we need from the database or a table. -
*
referred to as select star or asterisk or represents all. It defines that the query should return all columns of the queried tables. -
FROM
clause indicates the table(s) to retrieve data from. -
table_name
represents the table(s) name. -
column1, column2, ...
these are used to specify the columns from where we want to retrieve the data.
SELECT
statement is case insensitive, which means select
or SELECT
has the same result.Examples
We have a table named student_data that stores the id, name, and where the student lives.
public
schema. You can also display table from another specific schema. Click here for more info.#Case 1: Query data from all columns
- In the first case, we want to display all the data from the student_data table. Please refer to the syntax below:
- Use the
SELECT
statement within the table name to get all the data:
- If you have successfully run the query, you will get all the data from the student_data table.
#Case 2: Query data from specific columns
- We want to get the list of students’ names with their IDs. Please refer to the syntax below:
- Run the following query:
- If you have successfully run the query, you will get a list of students’ IDs & names from the student_data table.
#Case 3: Query data from a specific column with the condition
- If we have a large number of data, skimming for the desired data will require a long time. We can apply some conditions to the
SELECT
statement. Please refer to the syntax below:
- Let’s say we want to know the student’s name who lives in Sydney, have a look and run the query below:
- If you have successfully run the query, we now know that Will lives in Sydney.