Skip to main content

Viewing and Sorting Data in MySQL

Hands-On Lab

 

Photo of Matthew Pearson

Matthew Pearson

Linux Training Architect II in Content

Length

01:15:00

Difficulty

Intermediate

When working with databases, one of the most important skills is knowing how to view and arrange data to gain useful information. MySQL provides several ways to query databases in order to accomplish this. In this lab, we will run several queries in order to produce specific output.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Viewing and Sorting Data in MySQL

The Scenario

We have been asked to perform a series of queries, in order to provide information about the orders and products tables in the prod database.

Some additional Information:

  • The root MySQL user's password is: Linux4me!.
  • Do not update the password for the MySQL root user, as it is used for grading purposes.

Getting Logged In

Use the credentials and server IP in the hands-on lab overview page to log into our lab server. Refer back to that page for other usernames and passwords as we need them. Once we're logged into the server itself, log into MySQL as root:

[cloud_user@host]$ mysql -u root -p

Change to the prod database:

mysql> USE prod;

List All of the Rows in the orders and products Tables

Run the following query to list the rows in the orders table:

mysql> SELECT * FROM orders;

Run the following query to list the rows in the products table:

mysql> SELECT * FROM products;

Run a Query on the orders Table to Order the Rows by the purchaseDate Column in Ascending Order

mysql> SELECT * FROM orders ORDER BY purchaseDate;

Run a Query on the Orders Table That Shows All the Names Beginning with the Letter “m”

mysql> SELECT * FROM orders WHERE name LIKE 'm%';

Run a Query on the orders Table That Shows All Purchases in 2018 Where the productID Is Less Than 4

mysql> SELECT * FROM orders WHERE purchaseDate LIKE '2018%' AND productID < 4;

Run a Query on the products Table so That the Output Is in Alphabetical Order Based on the type Column

mysql> SELECT * FROM products ORDER BY type;

Run a Query on the products Table Where the Cost Is More Than or Equal to 1000

mysql> SELECT * FROM products WHERE cost >= 1000;

Run a Query on the products Table That Shows Output Where the Cost Is Greater Than 2000 or Less Than 500

mysql> SELECT * FROM products WHERE cost > 2000 OR cost < 500;

Show orders.name, products.type, and products.cost By Performing an Inner Join between orders and products Using orders.productID and products.id

mysql> SELECT orders.name, products.type, products.cost FROM orders INNER JOIN products WHERE orders.productID = products.id;

Conclusion

That's quite a bit of querying we did there, and some of them were rather complex. Congratulations!