Mechanics
- Once the server has verified that your username and password are correct, a database connection is generated for you to use.
- This connection is held by the application that requested it (which, in this case, is the mysql tool) until the application releases the connection (i.e., as a result of typing quit) or the server closes the connection (i.e., when the server is shut down).
- Each connection to the MySQL server is assigned an identifier, which is shown to you when you first log in.
Each time a query is sent to the server, the server checks, prior to statement execution:
- Do you have permission to execute the statement?
- Do you have permission to access the desired data?
- Is your statement syntax correct?
Once passed, your query will be handed to query optimizer, whose job it is to determine the most efficient way to execute your query. Finally, the result set will be returned after finished executing query.
Clauses
| select |
Determines which columns to include in the query’s result set |
| from |
Identifies the tables from which to retrieve data and how the tables should be joined |
| where |
Filters out unwanted data |
| group by |
Used to group rows together by common column values |
| having |
Filters out unwanted groups |
| order by |
Sorts the rows of the final result set by one or more columns |
Select
select * from person;
/* Basic */
select language_id,
'yo boy' as lang_usage,
language_id * 3.14 as lang_pi,
upper(name) as language_name
from language;
/* create a column alias after each element of your select clause, which included 3 labels. */
/* you can include as keyword, or not */
| language_id |
lang_usage |
lang_pi |
language_name |
| 1 |
yo boy |
3.14 |
ENGLISH |
| 2 |
yo boy |
6.28 |
ITALIAN |
| 3 |
yo boy |
9.42 |
JAPANESE |
| 4 |
yo boy |
12.56 |
MANDARIN |
| 5 |
yo boy |
15.7 |
FRENCH |
| 6 |
yo boy |
18.84 |
GERMAN |
select version (), user(), database()
/* Since this query simply calls three built-in functions and doesn’t retrieve data from any tables,
there is no need for a from clause */
| version() |
user() |
database() |
| 8.0.30 |
root@localhost |
sakila |
Removing Duplicates (in result set)
select distinct actor_id from film_actor order by actor_id;