Experiment No 1
Experiment No 1
Aim: To use simple Select statement to query the data from a single table
Theory: The SELECT statement allows to read data from one or more tables.
First, start with the SELECT keyword. It is called a keyword because it has a special meaning in MySQL,
in this case, SELECT instructs MySQL to retrieve data.
Next, have space and then a list of columns or expressions that you want to show in the result.
Then, the FROM keyword, space and the name of the table. Finally, a semicolon ; at the end of the
statement. The semicolon ( ;) is the statement delimiter. It specifies the end of a statement. If there are
two or more statements, use the semicolon ( ;) to separate them so that MySQL will execute each
statement individually.
In the SELECT statement, the SELECT and FROM are keywords and written in capital letters. Basically, it
is just about formating. The uppercase letters make the keywords stand out. Since SQL is not a case-
sensitive language, the keywords can be written in lowercase e.g., select and from, the code will still
run.
It is also important to note that the FROM keyword is on a new line. MySQL doesn’t require this.
However, placing the FROM keyword on a new line will make the query easier to read and simpler to
maintain.
When evaluating the SELECT statement, MySQL evaluates the FROM clause first and then
the SELECT clause:
1
MySQL SELECT statement examples
The table employees has eight columns: employee number, last name, first name, extension, email,
office code, reports to, and job title. It also has many rows as shown in the following picture
A) Using the MySQL SELECT statement to retrieve data from a single column example
The following example uses the SELECT statement to select the last names of tall employees:
2
The output of a SELECT statement is called results or a result set as it’s a set of data that results from a
query.
B) Using the MySQL SELECT statement to query data from multiple columns example
The following example uses the SELECT statement to get the first name, last name, and job title of
employees:
Even though the employees table has many columns, the SELECT statement just returns data of three
columns of all rows in the table as highlighted in the following picture
3
The following picture shows the result set:
C) Using the MySQL SELECT statement to retrieve data from all columns example
To retrieve data from all the columns of the employees table, all the column names can be specified in
the SELECT clause or just use the asterisk (*) shorthand as shown in the following query:
The SELECT * returns data from the columns that may not be used. It produces unnecessary I/O
disk and network traffic between the MySQL database server and application.
When explicitly the column names are specified , the result set is predictable and easier to
manage.
Using The SELECT * may expose sensitive information to unauthorized users