AppDividend
Latest Code Tutorials

SQL SELECT Query Example | SQL SELECT Statement

0

SQL SELECT statement is used to fetch the data from a database table that returns this data in the form of a result table. SELECT is used in SQL to fetch records from database tables, and you can do a lot many things using Select.

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. 

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

Content Overview

The SQL SELECT syntax is the 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

The above query fetches all the records from the defined table. In our case, it is Apps. The output is the following.

SQL SELECT Query Example

Finding how many rows in the table

Let’s count the rows of the table using the select statement.

select count(*) from Apps

See the output.

Finding how many rows in the table

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. See the output.

Select a few records based on some condition

Select a few columns instead of all columns

Let’s see the scenario where we do not need to fetch all the columns instead of 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 a few columns instead of all columns

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, the AppName column has already distinct values but, if it has repeated values, then it will eliminate other repeated values.

Select distinct (unique) records from Columns

 

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 value with condition based on

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';

See the output.

SELECT Query using BETWEEN, NOT BETWEEN

For NOT BETWEEN, you need to add the keyword NOT BETWEEN in the 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.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.