University of Technology Sydney SQL-Exam-Notes 2019 Database Fundamental

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SELECT Using “group by” you can categorize

List the columns (and expressions) to be your results into several groups, then
returned from the query analyse the data in each group based on Group by: Example
FROM your required information. Determine The Number Of Customers In
Indicate the table(s) or view(s) from Aggregate functions such as AVG, SUM, Each State.
which data will be obtained MIN, MAX and COUNT can be used to Select customerstate,
WHERE analyse the data in each group count(customerstate)
Indicate the conditions under which a row from customer_t
will be included in the result ROUND group by customerstate;
GROUP BY eg select round(avg(price), 2) from table1
Indicate categorization of results * will return avg price to 2 decimal points Group by clause: Rule 1
HAVING The first one (or more) columns
Indicate the conditions under which a NATURAL JOIN nominated in the “select” must also be
category (group) will be included nominated in the “GROUP BY”.
ORDER BY Equi-Join, one duplicate column is
Sorts the result according to specified eliminated and the remained is combined Group by clause: Rule 2
criteria into one table. The remaining columns nominated in the
“select” must be aggregate functions
Select column1, column2 Other joins requires specifying the (often “count”).
From Table1 column to merge on:
Where [Condition on rows] ON e.g. Having
Group by column1, column2 SELECT * FROM TableA INNER Used for queries on single or multiple
Having [Condition on groups] JOIN TableB tables
Order by column1 ON TableA.name = TableB.name For use with GROUP BY
Like a WHERE clause, but it operates
*Eliminating duplicate rows in result: INNER JOIN on groups (categories), not on
Distinct individual rows.
eg Select distinct(column1) From Table1 FROM Table-A INNER JOIN Table-B ________________________________
ON Table-A.column1 = TableB.column1 List the employees whose names
Comparison Operators Using in Where begin with an R.
Clause INNER JOIN clause is an alternative to select employeeid, employeename
WHERE clause, and is used to match from employee_t
primary and foreign keys. where employeename like 'R%';
An INNER join will only return rows ________________________________
from each table that have matching rows Which employees were hired during
in the other. 1999.
select employeeid, employeename
[LEFT, RIGHT, FULL, UNION] from employee_t
OUTER JOIN where employeedatehired between
Does not require matching columns '01-Jan-1999' and '31-Dec-1999';
produces a complete set of records from ________________________________
Table A, with the matching records
For each customer, list the
(where available) in Table B. If there is no
CustomerID and total number of
match, the other side will contain null.
orders.
select customerid, count(*)
Produce the set of records only in Table
A, but not in Table B. from order_t
To answer this question, perform the same group by customerid;
left outer join, then exclude the records ________________________________
we don't want from the right side via a For each customer, list the
*also, where clause. CustomerID and total number of
1. Between orders placed in March 2010.
2. And / Or Full outer join/Union join: includes all select customerid, count(*)
3. Like columns from each table , and an instance from order_t
4. is not null for each row of each table with matching where orderdate between '01-Mar-
5. In records from both sides where available. 2010' and '30-Mar-2010'
AND's are required If there is no match, the missing side will group by customerid;
OR's are optional (1 must be met) contain null. ________________________________
LIKE '%' <-- wildcard Write an SQL command that will find
IN (allows multiple LIKE) eg ('a', 'b', 'c') CROSS JOIN any customers who have not placed
SQL uses single quotes ' ' not double for orders.
strings. USES 'WHERE' select c.customerid from customer_t c left
DESC (descending order) join order_t o on
ASC (ascending order) FROM Table-A, JOIN Table-B c.customerid=o.customerid where
"\d" lists all tables in directory WHERE Table-A.column1 = o.customerid is null;
"\d table" lists table details TableB.column1 ________________________________
SELF JOIN
List the names and number of employee product_t p,orderline_t l, order_t o, List the salesperson who has sold the
supervised (label this value HeadCount) payment_t t where most computer desks.
for each supervisor who supervises more p.productid=l.productid and select salespersonid from
than two employees. l.orderid=o.orderid and salesperson_t o
select e1.employeename, count(*) as o.orderid=t.orderid group by t.orderid, where (select sum(orderedquantity)
HeadCount paymentamount order by Difference desc; from order_t natural join orderline_t
from employee_t e1 inner join ________________________________ where productid=(select productid
employee_t e2 Produce a list of all products (product from product_t where
description) and the number of times productdescription like '%Computer
on
each product has been ordered.
e1.employeeid=e2.employeesupervisor Desk%') and
select p.productid, productdescription,
group by 1 having count(*)>2; salespersonid=o.salespersonid group
count(o.productid) from product_t p left
________________________________ outer join orderline_t o on by salespersonid) >=all (select
List of supervisors and the number of p.productid=o.productid group by sum(orderedquantity) from order_t
employee supervised by them p.productid, p.productdescription; natural join orderline_t where
select e1.employeename, count(*) as ________________________________ productid=(select productid from
HeadCount List names of all employees who are product_t where productdescription
from employee_t e1 inner join managing people with skill ID BS12, like '%Computer Desk%') group by
employee_t e2 on e1.employeeid list each manger’s name only once, salespersonid);
=e2.employeesupervisor even if that manger manages several ________________________________
group by 1; people with this skill. Produce a list of all products (product
_______________________________ select e1.employeename from employee_t description) and the number of times
Write a query to display each item e1,employee_t e2 where each product has been ordered.
ordered in order number 1, its e2.employeesupervisor=e1.employeeid select productdescription,productid,
standard price, and total price for each and e2.employeeid in (select employeeid (select count(*) from orderline_t
item ordered. from employeeskills_t where group by productid having
select productdescription, skillid='BS12'); productid=p.productid) from
productstandardprice, orderedquantity, _______________________________ product_t p;
orderedquantity*productstandardprice as Display the employee ID and name for ________________________________
total_price those employees who do not possess the
Show customers ID and name for all
from product_t natural join orderline_t skill Router. the customers who have ordered both
where orderid=1; select employeeid, employeename product IDs 3 and 4 on the same order.
_______________________________ from employee_t where employeeid select customerid, customername
Write an SQL command to total the not in (select employeeid from from customer_t where customerid in
cost of order number 1. employeeskills_t where skillid=(select (select customerid from order_t where
select sum skillid from skill_t where orderid in (select orderid from order_t
(orderedquantity*productstandardprice) skilldescription='Router')); o where 3 in (select productid from
from product_t natural join orderline_t ________________________________ orderline_t where orderid=o.orderid)
where orderid=1; Calculate the number of computer and 4 in (select productid from
_______________________________ desks sold by each salesperson.
Calculate the total raw material cost orderline_t where
select salespersonid, orderid=o.orderid)));
for each product compared to its sum(orderedquantity) from order_t
standard product price. Display ________________________________
natural join orderline_t where
product description, standard price productid=(select productid from
and the total cost in the result. product_t where productdescription
select productdescription,
like '%Computer Desk%') group by 1;
productstandardprice,
________________________________ ________________________________
quantityrequired*materialstandardprice as
List the product ID and the total
TotCost
amount ordered of that product by the
from product_t p inner join uses_t u on
customer who has bought the most of
p.productid=u.productid inner join
rawmaterial_t r on that product.
Select p.productid, o.customerid,
u.materialid=r.materialid;
________________________________ sum(orderedquantity) from ________________________________
For every order that has been received, orderline_t p natural join order_t o
display the order ID, the total dollar where o.customerid in (select
amount owed on that order, and the customerid from customer_t c where
amount received in payments on that (select sum(orderedquantity) from
order. List the results in decreasing order_t natural join orderline_t where
order of the difference between total productid= p.productid group by ________________________________
due and amount paid. customerid having
select t.orderid,sum customerid=c.customerid) >=all (select
(orderedquantity*productstandardprice) sum(orderedquantity) from order_t
as TotalOwed, paymentamount, natural join orderline_t where
sum(orderedquantity*productstandardpric productid=p.productid group by
e)-paymentamount as Difference from customerid)) group by 1,2;

You might also like