Skip to content

[MySQL] Note(1) SELECT, FROM, WHERE, GROUP-BY, HAVING, ORDER-BY, LIMIT

The article records how to use the following SQL instructions of:

  • SELECT
  • FROM
  • WHERE
  • GROUP BY
  • HAVING
  • ORDER BY
  • LIMIT

There is a priority order for these 7 instructions, as shown in the figure:

The meaning of each instruction is:

InstructionDescription
SELECT欲查詢的欄位
FROM遇查詢的表格
WHERE查詢條件
GROUP欄位組別
HAVING組別條件
ORDER BY顯示順序
LIMIT顯示限制

In this case, it may still be unclear how to operate, so let’s take a look some example.


Switch Database

First, open MySQL Workbench. If you have other programs that can practice SQL, they can also be used.

If you don’t have any programs like MySQL installed on your computer, maybe you can refer to: MySQL download and installation steps note

show databases;



Output:

information_schema
mysql
performance_schema
sakila
sys
world 

Here we first select the “world” database.

use world;



So we switch to this “world” database.

Then we show the tables in the database:

show tables;



Output:

city
country
countrylanguage 

If there are these tables, it means that there is no problem with the default database.


SELECT

FROM

These two instructions need to be used together. SELECT is followed by which column to select, and FROM is followed by which table we are querying.

For example:

select * from city;



Output:

IDNameCountryDistrictPopulation
1KabulAFGKabol1780000
2QandaharAFGQandahar237500
3HeratAFGHerat186800
4Mazar-e-SharifAFGBalkh127800
5AmsterdamNLDNoord-Holland731200

I omitted some information to show the results.

We just used * to print all the fields; you can also select only the field names you want to query.

select ID from city;



Output:

ID
129
1
2
3
4
56

In this way, we only have to print out the ID field.

If you want to print multiple fields:

select ID, Name from city;



Output:

IDName
1Kabul
2Qandahar
3Herat
4Mazar-e-Sharif
5Amsterdam

WHERE

WHERE instruction like if in other programming languages, is used to make conditional judgments. For example, the program names we set to print all start with CountryCode as “AFG“.

select Name, CountryCode from city where CountryCode = 'AFG';



Output:

NameCountryCode
KabulAFG
QandaharAFG
HeratAFG
Mazar-e-SharifAFG

GROUP BY

GROUP BY is more difficult to explain. For example, we can group the returned results.

select
    Continent, Name, max(GNP)
    from country
    group by Continent;



Output:

ContinentNamemax(GNP)
North AmericaAruba8510700.00
AsiaAfghanistan3787042.00
EuropeAlbania2133367.00
South AmericaArgentina776739.00
OceaniaAmerican Samoa351182.00
AfricaAngola116729.00
AntarcticaAntarctica0.00

HAVING

HAVING is to look for eligible conditions. For example, we take the example just now and continue to do it. If today we want to see the result only with GNP exceeding 500,000, we can add the HAVING instruction at the end.

select
    Continent, Name, max(GNP)
    from country
    group by Continent
    having max(GNP) > 500000;



Output:

ContinentNamemax(GNP)
North AmericaAruba8510700.00
AsiaAfghanistan3787042.00
EuropeAlbania2133367.00
South AmericaArgentina776739.00

We filtered out data with a smaller GNP.


ORDER BY

ORDER BY, as the name implies, is to sort the data display method. For example, for the information we just query, we use Capital to sort.

select
    Capital, Continent, Name, max(GNP)
    from country
    group by Continent
    order by Capital desc;



Output:

Capital Continent Name max(GNP)
129North AmericaAruba8510700.00
69South AmericaArgentina776739.00
56AfricaAngola116729.00
54OceaniaAmerican Samoa351182.00
34EuropeAlbania2133367.00
1AsiaAfghanistan3787042.00
NULLAntarcticaAntarctica0.00

We can see that the current sorting method is to sort Capital from largest to smallest.


LIMIT

LIMIT is a instruction to limit the number of displayed data.

select
    Capital, Continent, Name, max(GNP)
    from country
    group by Continent
    order by Capital desc
    limit 2;



Output:

CapitialContinentNamemax(GNP)
129North AmericaAruba8510700.00
69South AmericaArgentina776739.00

we set LIMIT to 2, and our result will only print 2 data.

Leave a Reply