Important SQL Queries with Examples
Learning SQL from time to
time is important for depth SQL knowledge. Here we are going to discuss the
commonly utilized SQL queries and important ones. When you review this list of
SQL queries, you can easily find them mainly for beginners.
Acquiring data from particular columns -
This
query is going to display every detail present from the table bird. If you wish
to retrieve the data from particular columns list them after SELECT. Here we
are acquiring the information from id and name columns.
SELECT
id, name
FROM
bird;
Filtering of data using conditions joined by AND operator –
If
you desire the data utilizing more than one condition then you can utilize AND
operator. In the below example, we are looking for the records having a value
of 2 or more in the column age and parrot in the column name.
SELECT
id, name, age
FROM
bird
Where
age>=2 AND name= ‘parrot’;
Utilizing DISTINCT to retrieve non repeated records –
You
can put DISTINCT after the SELECT for retrieving only each kind of record. In
the below example, suppose we wish to retrieve records from columns name and
color. If the value from these specific columns is similar in more than one
record. The query also can be able to return only one of these records.
SELECT
DISTINCT name, color
FROM
country;
Sorting of data according to one column –
For
sorting data according to one column put the column name after ORDER BY. The
default sorting method is done in alphabetic order. But you can also display
rows in the order of descending by adding DESC after the column name. In the
below example, we are going to sort the data in columns id and name according
to the column name.
SELECT
id, name
FROM
bird
ORDER
BY name;
Looking for values that match a certain pattern –
You
can use LIKE for searching the values which match a particular pattern. In the
below example, we are going to retrieve records from columns id and name which
store a strong comprising of a character f in the column name.
SELECT
id, name
FROM
bird
WHERE
name LIKE %f%;
Utilizing mathematical operators –
You
can also write queries that perform mathematical calculations using
mathematical operators such as + or -. In the below example let's calculate the
discounted price.
SELECT
price – discount
FROM
product;
Finding the intersection of sets and data –
The
element INTERSECT returns the intersection of sets and data. Let's see an
example.
SELECT
first-name FROM employee
INTERSECT
SELECT
first-name FROM customer;
To count the number of rows in a table –
You
can use COUNT for counting the number of rows present in a table.
SELECT
COUNT (id)
FROM
product;
In
the above example, it returns the number of values starting from the column id
and which is stored in the table product.
Calculation of sum of values in a column –
The
SUM calculates the sum of values present in a column. Here it returns the
values of all the items.
SELECT
SUM (price)
FROM
item;
Thus,
these are some of the SQL queries which you need to learn to have basic SQL
knowledge.
Read more articles here -
0 Comments