0% found this document useful (0 votes)
44 views4 pages

Exploring The Database and Schema

This document provides examples of SQL queries on an orders database table to: 1) Preview the first 7 rows and columns of the orders table. 2) Retrieve a sample of 6 rows showing the city, state, and region to verify store locations. 3) Calculate estimated sales tax by multiplying sales by 5% and aliasing the new column.

Uploaded by

Fagbohunka Eric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Exploring The Database and Schema

This document provides examples of SQL queries on an orders database table to: 1) Preview the first 7 rows and columns of the orders table. 2) Retrieve a sample of 6 rows showing the city, state, and region to verify store locations. 3) Calculate estimated sales tax by multiplying sales by 5% and aliasing the new column.

Uploaded by

Fagbohunka Eric
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPLORING THE DATABASE AND SCHEMA

Worked Exercises
SELECT *
FROM orders

SELECT *
FROM orders;

1. Write a query to recall all columns and the first 7 rows from orders table to
preview what the table looks like.
SELECT *
FROM orders
LIMIT 7;

* is a special character meaning “all”.


FROM orders tells SQL which table to read from.
LIMIT 7 returns the number of rows to 7.

A database table consists of rows and columns which are often called records
and fields.
We can reduce the number of records (rows) by using the LIMIT clause.

SELECT *
FROM orders
LIMIT 5;

We can reduce the number of fields (columns) by listing them in the SELECT
clause, separated by commas.

SELECT order_id, order_date, customer_name, category


FROM orders;
2. Your Supervisor at ErikomStores has asked you to create a list of location and
regions from the orders table so managers can verify which cities and states
they are assigned to.
a. Write a query to ask the ErikomStore DB to show the city, state, and
region field from the orders table.
b. Your Supervisor only needs to see a sample of the data, so limit your
results to 6 records.

SELECT city, state, region


FROM orders
LIMIT 6;

ALIAS or Rename
The headers of a table can be renamed if desired.
This task is possible by using SELECT Clause with AS

3. Write a query to pull the order_id, sales, and multiply sales by 5% to estimate
sales tax.

SELECT order_id
sales,
sales * 0.05 AS sales_tax
FROM orders;

4. Your manager has requested you find the profit margin on all orders from the
orders table.
a. Write a query that includes order_id, sales, profit, and profit margin
(profit divided by sales).
b. Alias the calculated fields as profit_margin.
c. Only display 5 rows.

SELECT order_id
sales,
profit,
profit/sales AS profit_margin
FROM orders
LIMIT 5;
ROUND ( )
ROUND () helps to round numerical values to a specified number of decimal
places.

Syntax:
ROUND (value, decimal_places)

5. The management of ErikomStores is concerned about many customers that


bought multiples of the same items which makes it difficult to see how much
each item costs. In view of this scenario, your Supervisor has assigned you to
a. Write a query from the orders table that include order_id, sales, and
quantity.
b. Create a field price_per_unit that divides sales by quantity.
c. Round price_per_unit to the nearest cent.
d. Only display 8 rows.

SELECT order_id,
sales,
quantity,
Round (sales/quantity, 2) AS price_per_unit
FROM orders
LIMIT 8;
CONCATENATION
It is possible to concatenate, or combine, multiple fields together as one field.
Concatenate Operator (| |) is used to join two strings into one.

SELECT city ||’, ‘|| state AS location


FROM orders;

6. Every ErikomStores location is named after the city where it is located. For
example, store in Los Angeles, California is called “ErikomStore Los Angeles”.
a. Write a query that includes order_id, state, and region
b. Create a new column called local_store that concatenate the word
“ErikomStore” with city.
c. Limit your results in 4 rows.

SELECT order_id,
state,
region,
ErikomStore ||’, ‘|| city AS local_store
FROM orders
LIMIT 4;

You might also like