create account

Clauses, operators, and functions in SQL by darienken

View this thread on: hive.blogpeakd.comecency.com
· @darienken ·
$0.62
Clauses, operators, and functions in SQL
Clauses
¿What can we do with them?
Depending on what clause we’re going to use, we can do different things, there are five clauses that we can use:

From
We use it to set the table where a field comes from

![1_3kNCAsOFnUNZ2FzqtlG-ig.png](https://images.hive.blog/DQmbMD16xhdMZTFRvTFdMwQSfsHixfEAJs3eGqNprBFSime/1_3kNCAsOFnUNZ2FzqtlG-ig.png)

Where
It is used to set criteria or conditions for rows, it is like a filter for rows

![1_3kNCAsOFnUNZ2FzqtlG-ig (1).png](https://images.hive.blog/DQmbMD16xhdMZTFRvTFdMwQSfsHixfEAJs3eGqNprBFSime/1_3kNCAsOFnUNZ2FzqtlG-ig%20(1).png)

Group By
It can group rows by a field

Having
It is used to set criteria or conditions for a group of rows, it is like a filter for grouped rows.

Order by
It can order rows by a field, by default, all the rows are ordered by the first field.

![1_Y9efaolkxg6JXXE4Mz3AjQ.png](https://images.hive.blog/DQmNr4cBYQ4w4Rx3Yk8ZX4sEy1NwVEU4SXcVWSd84Luzqjq/1_Y9efaolkxg6JXXE4Mz3AjQ.png)

As you can see in the picture the rows are ordered by the fecha field, by default the clause “order by” will order the rows of text type from a to z, the rows of date type from the oldest year to the youngest year, and the rows of number type from 0 to latest number if we want to change this order we can use the “desc” instruction.

¿When to use it?
After we use a command and when we want to query for more specific information and also there is an order to follow when we use clauses the order is command + from + where + group by + having + order by, there is no need to use all the clauses it depends in what we want to query

Operators
After using clauses we can use operators, operators are used to set a more specific query.
There are two types of operators, the logical operators, and the comparison operators.

Logical Operators

And
It can concatenate criterias

![1_fQQkExEHRbxKRC7iaX3NyA.png](https://images.hive.blog/DQmRxUhJaBDC68thmYm7ErfwLy16fiLmT4q23BEj5VUAcgp/1_fQQkExEHRbxKRC7iaX3NyA.png)

Or
It can concatenate instructions with the same criteria

![1_ZxYmfqL55HHb9VUHrsk4pg.png](https://images.hive.blog/DQmbL3eHhS6Txmu7oMkxutYCCXra3pWa7gB8G5bjK1U4smS/1_ZxYmfqL55HHb9VUHrsk4pg.png)


Not
Not logic

Comparison Operators
Greater than
'>
Less than
<
Greater than or equal to
≥
Less than or equal to
≤
Equal to
=
Not equal to
<>

Between
It is used to set a range of conditions

![1_c5depa1H214GI4l2EXmFkg.png](https://images.hive.blog/DQmUX34JhQsYAH444CXZu3eUC2H39E2DeVNdBFXzNodX4sX/1_c5depa1H214GI4l2EXmFkg.png)

As you can see if we want to use “between ”also we have to use the “and” operator.

Like
Used with wildcard characters

In
Used to specifying rows in a determinate field.

Grouped queries or total queries
¿What is a grouped query?
It is a query where we get a total result of rows that are grouped.

¿When to do a grouped query?
When we want a total result of rows that are grouped like for example… A query where we need to know the total of persons of a field or the max value of a field of values.

¿How to do a grouped query?
We need functions and fields, we need two fields, one field is going to be the field where rows are going to be grouped and the other field is going to be the field where we’re going to get the totals, so a field for rows and other for totals.

Alias and names inside a query
What is an alias?
It is a way in how we can change the name of a column in a query, I said query because just we are changing the name inside a query if we change the name of a field inside a query that doesn’t affect the original column’s name if we have an alias inside a query we need to use the alias and not the original name.

Aggregation functions
We have functions called aggregation functions that are used to make grouped queries, aggregation functions are:

AVG
We used it to get the average of a field.

![1_APg_EHUznxLgCJuElR-gew.png](https://images.hive.blog/DQmWyJHh8XFwZQ2qB5kLrGc15xQ8aipLVGgSxdc1dPgrRtE/1_APg_EHUznxLgCJuElR-gew.png)

Count
We used it to count the number of rows in a field.

Sum
We used it to sum the rows of a field.

Max
We used it to get the max of rows of a field.

Min
We used it to get the min of rows of a field.

Extra important information
Where clause select rows to display before they been grouped for an aggregation function while having clause select rows to display after they been grouped for an aggregation function.

When we use the aggregation functions we use the having clause, also we can use the where clause but there is no logic in use it, because “where” is used for setting criteria for rows that are not inside a group, instead of using “where” in a query where we have aggregation functions, we can use “having” that is used for setting criteria for rows that are inside a group.

![1_ruy7RV2J5AwMwp3GfSKzWw.png](https://images.hive.blog/DQmfVGYzmfXi3Hp6TiqouZuUCJzcZ4wgrBRhJgmsmaFhFWF/1_ruy7RV2J5AwMwp3GfSKzWw.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authordarienken
permlinkclauses-operators-and-functions-in-sql
categorysql
json_metadata"{"tags":["sql","mysql","programming","database","engineering"],"image":["https://images.hive.blog/DQmbMD16xhdMZTFRvTFdMwQSfsHixfEAJs3eGqNprBFSime/1_3kNCAsOFnUNZ2FzqtlG-ig.png","https://images.hive.blog/DQmbMD16xhdMZTFRvTFdMwQSfsHixfEAJs3eGqNprBFSime/1_3kNCAsOFnUNZ2FzqtlG-ig%20(1).png","https://images.hive.blog/DQmNr4cBYQ4w4Rx3Yk8ZX4sEy1NwVEU4SXcVWSd84Luzqjq/1_Y9efaolkxg6JXXE4Mz3AjQ.png","https://images.hive.blog/DQmRxUhJaBDC68thmYm7ErfwLy16fiLmT4q23BEj5VUAcgp/1_fQQkExEHRbxKRC7iaX3NyA.png","https://images.hive.blog/DQmbL3eHhS6Txmu7oMkxutYCCXra3pWa7gB8G5bjK1U4smS/1_ZxYmfqL55HHb9VUHrsk4pg.png","https://images.hive.blog/DQmUX34JhQsYAH444CXZu3eUC2H39E2DeVNdBFXzNodX4sX/1_c5depa1H214GI4l2EXmFkg.png","https://images.hive.blog/DQmWyJHh8XFwZQ2qB5kLrGc15xQ8aipLVGgSxdc1dPgrRtE/1_APg_EHUznxLgCJuElR-gew.png","https://images.hive.blog/DQmfVGYzmfXi3Hp6TiqouZuUCJzcZ4wgrBRhJgmsmaFhFWF/1_ruy7RV2J5AwMwp3GfSKzWw.png"],"app":"hiveblog/0.1","format":"markdown","description":"Clauses, operators, and functions in SQL. Clauses ¿What can we do with them? Depending on what clause we’re going to use..."}"
created2021-10-02 04:21:30
last_update2021-10-02 04:21:30
depth0
children1
last_payout2021-10-09 04:21:30
cashout_time1969-12-31 23:59:59
total_payout_value0.317 HBD
curator_payout_value0.303 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,876
author_reputation7,051,097,276
root_title"Clauses, operators, and functions in SQL"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id106,714,398
net_rshares500,200,062,400
author_curate_reward""
vote details (38)
@hivebuzz ·
Congratulations @darienken! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@darienken/upvoted.png?202110020728"></td><td>You received more than 10 upvotes.<br>Your next target is to reach 50 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@darienken) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/pum-202109-final"><img src="https://images.hive.blog/64x128/https://i.imgur.com/lpF7k06.png"></a></td><td><a href="/hivebuzz/@hivebuzz/pum-202109-final">Hive Power Up Month Challenge - Winners List</a></td></tr><tr><td><a href="/hivebuzz/@hivebuzz/pum-202109-29"><img src="https://images.hive.blog/64x128/https://i.imgur.com/rv2OQ5F.png"></a></td><td><a href="/hivebuzz/@hivebuzz/pum-202109-29">Hive Power Up Month - Feedback from Day 29</a></td></tr><tr><td><a href="/hivebuzz/@hivebuzz/pud-202110"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-202110">Hive Power Up Day - October 1st 2021 - Hive Power Delegation</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-darienken-20211002t074128
categorysql
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-10-02 07:41:27
last_update2021-10-02 07:41:27
depth1
children0
last_payout2021-10-09 07:41:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,429
author_reputation370,687,705,918,137
root_title"Clauses, operators, and functions in SQL"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id106,717,019
net_rshares0