Professional Documents
Culture Documents
-It is also important to note that FROM keyword is on newline. My SQL doesn’t require this.
However, placing the FROM keyword on new line maker easier to read query.
Employees Table
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:
And we write Query:
SELECT lastName
FROM employees;
firstname,
jobtitle
FROM employees;
FROM employee;
The SELECT * returns data from the columns that you may not use. It produces
unnecessary I/O disk and network traffic between the MySQL database server and
application.
When you explicitly specify the column names, the result set is predictable and
easier to manage. However, if you use the SELECT * and someone changes the table
by adding more columns, you will end up with a result set that is different from the one
that you expected.
FROM table_name
-When you use the select statement to query data from a table, the result set is not sorted. It
means that the rows in the result set can be in any order. To sort the result set, you add
the ORDER BY clause to the SELECT statement.
-The ASC stands for ascending and the DESC stands for descending. You use ASC to sort the
result set in ascending order and DESC to sort the result set in descending order.
-By default, the ORDER BY clause uses ASC if you don’t explicitly specify any option.
Therefore, the following clauses are equivalent:
and
ORDER BY column1;
-If you want to sort the result set by multiple columns, you specify a comma-separated list
of columns in the ORDER BY clause:
ORDER BY
column1 ASC,
column2 DESC;
-It is possible to sort the result by a column in ascending order, and then by another column
in descending order:
ORDER BY
column1 ASC,
column2 DESC;
My SQL Ordered By Examples with Tables
SELECT contactLastname,
contactFirstname
FROM customers
ORDER BY contactLastname;
If you want to sort customers by the last name in the descending order, you use
the DESC after the contactLastname column in the ORDER BY clause as shown in the
following query:
SELECT contactLastname,
contactFirstname
FROM customers
ORDER BY contactLastname DESC;
The Output Will be:
If you want to sort the customers by the last name in descending order and then by the first
name in ascending order, you specify both DESC and ASC in the corresponding column as
follows:
SELECT
contactLastname,
contactFirstname
FROM
customers
ORDER BY
contactLastname DESC,
contactFirstname ASC;
SELECT orderNumber,
orderlinenumber,
quantityOrdered * priceEach
FROM orderdetails
ORDER BY quantityOrdered * priceEach DESC;
To make the query more readable, you can assign the expression in the SELECT clause
a column alias and use that column alias in the ORDER BY clause as shown in the following
query:
SELECT
orderNumber,
orderLineNumber,
quantityOrdered * priceEach AS subtotal
FROM
orderdetails
ORDER BY subtotal DESC;
Using SQL Order By to sort data using custom list
The ORDER BY clause allows you to sort data using a custom list by using
the FIELD() function.
Suppose that you want to sort the sales orders based on their statuses in the following
order:
In Process
On Hold
Canceled
Resolved
Disputed
Shipped
To do this, you can use the FIELD() function to map each order status to a number and
sort the result by the result of the FIELD() function:
FROM orders
The WHERE clause allows you to specify a search condition for the rows returned by a query.
The following shows the syntax of the WHERE clause:
SELECT
select_list
FROM
table_name
WHERE
search_condition;
MySQL WHERE clause examples
The following query uses the WHERE clause to find all employees whose job titles are Sales
Rep:
SELECT
lastname,
firstname,
jobtitle
FROM
employees
WHERE
jobtitle = 'Sales Rep';
This query finds employees whose job title is Sales Rep or employees who locate the office
with office code 1:
SELECT
lastName,
firstName,
jobTitle,
officeCode
FROM
employees
WHERE
jobtitle = 'Sales Rep' OR
officeCode = 1
ORDER BY
officeCode ,
jobTitle;
The following query finds employees who locate in offices whose office code is from 1 to 3:
SELECT
firstName,
lastName,
officeCode
FROM
employees
WHERE
officeCode BETWEEN 1 AND 3
ORDER BY officeCode;
SELECT
firstName,
lastName
FROM
employees
WHERE
lastName LIKE '%son'
ORDER BY firstName;
The Output will be:
SELECT
firstName,
lastName,
officeCode
FROM
employees
WHERE
officeCode IN (1 , 2, 3)
ORDER BY
officeCode;
To check if a value is NULL or not, you use the IS NULL operator, not the equal operator (=).
The IS NULL operator returns TRUE if a value is NULL.
value IS NULL
In the database world, NULL is a marker that indicates a piece of information is missing or
unknown. It is not equivalent to the number 0 or an empty string.
This statement uses the WHERE clause with the IS NULL operator to get the row whose
value in the reportsTo column is NULL:
SELECT
lastName,
firstName,
reportsTo
FROM
employees
WHERE
reportsTo IS NULL;
Operator Description
= Equal to. You can use it with almost any data types.
< Less than. You typically use it with numeric and date/time data types.
The following query uses the not equal to (<>) operator to find all employees who are not
the Sales Rep:
SELECT
lastname,
firstname,
jobtitle
FROM
employees
WHERE
jobtitle <> 'Sales Rep';
The following query finds employees whose office code is greater than 5:
SELECT
lastname,
firstname,
officeCode
FROM
employees
WHERE
officecode > 5;
The following query returns employees with office code less than or equal 4 (<=4):
SELECT
lastname,
firstname,
officeCode
FROM
employees
WHERE
officecode <= 4;
The percentage ( % ) wildcard matches any string of zero or more characters.
MySQL LIKE operator examples
Let’s practice with some examples of using the LIKE operator. We will use the
following employees table from the sample database for the demonstration:
A) Using MySQL LIKE with the percentage (%) wildcard examples
This example uses the LIKE operator to find employees whose first names start with a:
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
firstName LIKE 'a%';
In this example, MySQL scans the whole employees table to find employees whose first
names start with the character a and are followed by any number of characters.
This example uses the LIKE operator to find employees whose last names end
with on e.g., Patterson, Thompson:
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
lastName LIKE '%on';
If you know the searched string is embedded inside in the middle of a string, you can use
the percentage ( % ) wildcard at the beginning and the end of the pattern.
For example, to find all employees whose last names contain on , you use the following
query with the pattern %on%
ssss
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
lastname LIKE '%on%';
To find employees whose first names start with T , end with m, and contain any single
character between e.g., Tom , Tim, you use the underscore (_) wildcard to construct the
pattern as follows:
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
firstname LIKE 'T_m';
The MySQL allows you to combine the NOT operator with the LIKE operator to find a string
that does not match a specific pattern.
Suppose you want to search for employees whose last names don’t start with the
character B, you can use the NOT LIKE with a pattern as shown in the following query:
SELECT
employeeNumber,
lastName,
firstName
FROM
employees
WHERE
lastName NOT LIKE 'B%';
MySQL LIKE operator with ESCAPE clause
Sometimes the pattern, which you want to match, contains wildcard character e.g., 10%,
_20, etc. In this case, you can use the ESCAPE clause to specify the escape character so
that MySQL will interpret the wildcard character as a literal character. If you don’t specify
the escape character explicitly, the backslash character \ is the default escape character.
For example, if you want to find products whose product codes contain the string _20 , you
can use the pattern %\_20% as shown in the following query:
SELECT
productCode,
productName
FROM
products
WHERE
productCode LIKE '%\_20%';