SQL SELECT Query: The Complete Guide

2
283
SQL SELECT Query Example | SQL SELECT Statement Tutorial

Select query in SQL is one of the most powerful and heavily used commands. This is the first command anyone learns in SQL, even before CREATE, which is used to create a table in SQL.

SQL SELECT

SQL SELECT statement is used to fetch the data from a database table in the form of a result table. For example, you can select all records; choose a few based on the condition specified in the WHERE clause, select all columns using the wild card (*), or only select a few columns by explicitly declaring them in a query.

Syntax

SELECT column-names
  FROM table-name

Here, column names are the field names of the table you want to select data from. If you select all the fields in the table, use the following syntax.

SELECT * FROM table_name;

Here are some of the fundamental examples of SQL SELECT Query Statements.

How to fetch all records in SQL

To fetch all the records in SQL, use the Select * from tablename query.

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

How to count rows in SQL

To count the rows in the table in SQL, use the COUNT() function.

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

How to select records based on conditions in SQL

To select filtered records from a table in SQL, use the SQL WHERE statement. Check out the SQL WHERE Clause if you are unaware of the WHERE Statement.

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 the 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

How to select unique records from columns in SQL

To select unique records from table columns in SQL, use the select distinct statement. Let’s take an example where we need to find unique records based on the column values.

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.

That’s it for this tutorial.

2 Comments

Leave A Reply

Please enter your comment!
Please enter your name here

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