SQL SELECT Query Example | SQL SELECT Statement Tutorial
SQL SELECT Query Example | SQL SELECT Statement Tutorial is today’s topic. The Select is the most commonly used statement in SQL. SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of a result table.
The Select command in SQL is one of the most powerful and heavily used commands. This is I guess the first command anyone learns in SQL even before CREATE which is used to create a table in SQL. SELECT is used in SQL to fetch records from database tables, and you can do a lot many things using Select.
For example, you can select all records; you can choose few records based on the condition specified in WHERE clause, select all columns using the wild card (*) or only selecting a few columns by explicitly declaring them in a query.
SQL SELECT Query Example
- 1 SQL SELECT Query Example
- 2 Fetch all the records
- 3 Finding how many rows in the table
- 4 Select a few records based on some condition
- 5 Select a few columns instead of all columns
- 6 select distinct (unique) records from columns
- 7 Select value with condition based on >, <, >=, <=
- 8 select query using BETWEEN, NOT BETWEEN
The SQL SELECT syntax is following.
SELECT column-names FROM table-name
Here, column-names are the field names of the table you want to select data from. If you’re going to select all the fields available in the table, use the following syntax.
SELECT * FROM table_name;
Here are some of the fundamental examples of SQL SELECT Query Statements.
Fetch all the records
Select * from Apps
Above query fetch all the records from the defined table. In our case, it is Apps. The output is following.
Finding how many rows in the table
Let’s count the rows of the table using the select statement.
select count(*) from Apps
The output is following.
Select a few records based on some condition
If you are not aware of WHERE Statement then check out SQL WHERE Clause Example.
See the following query.
select * from Apps where AppName = 'MoneyControl'
In this query, we are fetching the records based on where condition. We are fetching only those records, whose AppName is MoneyControl. The output is following.
Select a few columns instead of all columns
Let’s see the scenario where we do not need to fetch all the columns instead only required columns. See the below query.
select AppName, AppCategory from Apps
We are only selecting the AppName and AppCategory from the Apps table. See the output.
select distinct (unique) records from columns
Let’s see the scenario, where we need to find unique records based on the column values. See the following query.
select distinct AppName from Apps
In our table, AppName column has already distinct values but, if it has repeated values, then it will eliminate other repeated values.
Select value with condition based on >, <, >=, <=
We can select value with condition based on >, <, >=, <=. See the following code.
select AppName from Apps where AppPrice > 60
See the following example.
select query using BETWEEN, NOT BETWEEN
As the name suggests BETWEEN is used to get data between ranges.
select * from Apps where AppPrice BETWEEN '70' AND '100';
The output is following.
For NOT BETWEEN, you need to add the keyword NOT BETWEEN in above query instead of BETWEEN. It will give us the output which is not in range.
Finally, SQL SELECT Query Example | SQL SELECT Statement Tutorial is over.