Database Essentials 18S PDF
Database Essentials 18S PDF
•
•
•
•
•
•
•
Part
•
•
Part
Warehouse
Stock
Person
Person
Person
Person
Stock
Stock/Part Description View
•
•
•
•
•
•
•
✓ ✓
✓ ✓
✓
✓ ✓
✓ ✓
✓
✓
✓
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
SELECT SalesOffice
FROM SalesStaff
WHERE Customer1 = ‘Ford’ OR
Customer2 = ‘Ford’ OR
Customer3 = ‘Ford’
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
CREATE TABLE users (
id INTEGER,
name TEXT,
join_date DATE
);
INSERT INTO users (id, name, join_date) VALUES (1, 'Lionel', '2013-12-
29');
INSERT INTO USERS (id, name, join_date) VALUES (2, 'Fred', '2013-12-
30');
INSERT INTO USERS (id, name, join_date) VALUES (3, 'Jane', '2013-12-
29'), (5, 'Dan', '2013-11-20');
id, name
1, 'Lionel'
3, 'Jane'
id, name
3, 'Jane'
1, 'Lionel'
SELECT id, name FROM users WHERE join_date = '2013-12-29' ORDER BY id;
•
•
•
DROP employees;
SELECT * FROM account_type;
INSERT INTO employees
(
username,
password,
full_name
)
VALUES
(
‘johnd’,
‘mypass’,
‘John Doe’
);
UPDATE accounts
SET phone='615551234',
fullname='Dan G'
WHERE phone='615551212';
Total
-----------
1837
SELECT AVG(unit_price * quantity) As AveragePrice
FROM products
WHERE continent = “North America”
AveragePrice
---------------------
862.3075
Number of continents
--------------------
7
Number of continents
--------------------
3
Largest Order
---------------------
2517.58
SELECT continent, MIN(quantity * unit_price) AS 'Smallest Order'
FROM products
GROUP BY continent
+----+--------+-----+---------+---------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+--------+-----+---------+---------+
| 2 | Khilan | 25 | Delhi | 1500.00 |
+----+--------+-----+---------+---------+
Table A Table B
id name id name
-- ---- -- ----
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
id name id name
-- ---- -- ----
1 Pirate 2 Pirate
3 Ninja 4 Ninja
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
id name id name
-- ---- -- ----
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
id name id name
-- ---- -- ----
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
id name id name
-- ---- -- ----
2 Monkey null null
4 Spaghetti null null
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null
id name id name
-- ---- -- ----
2 Monkey null null
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
select top 1 orderid,
convert(char(10), orderdate,121) last_paris_order,
(select convert(char(10),max(orderdate),121)
from orders) last_orderdate,
datediff(dd,orderdate,(select max(orderdate)
from orders)) day_diff
from orders
where shipcity = 'paris'
order by orderdate desc
select au_lname,
au_fname,
title from (select au_lname, au_fname, au_id
from authors
where state = 'ca') as a
join titleauthor ta on a.au_id = ta.au_id
join titles t on ta.title_id = t.title_id
select pub_name,
count(*) bookcnt
from titles t
join publishers p on t.pub_id = p.pub_id
group by pub_name
having p.pub_name in (select pub_name
from publishers
where state <> 'ca')
select distinct orderid
from orderdetails od
where quantity > (select avg(quantity) * .1
from orderdetails
where od.productid = productid)
select c1.companyname,
c1.contactname,
c1.address,
c1.city,
c1.country,
c1.postalcode
from customers c1
where c1.customerid in (select top 2 c2.customerid
from orderdetails od
join orders o on od.orderid = o.orderid
join customers c2 on o.customerid =
c2.customerid
where c2.region = c1.region
group by c2.region, c2.customerid
order by sum(od.unitprice * od.quantity * (1 -
od.discount)) desc)
order by c1.region
select c.customerid,
count(*) * 75 rebate
from customers c
join orders o on c.customerid = o.customerid
where datepart(yy,orderdate) = '1998'
group by c.customerid
having 750 < all(select sum(unitprice * quantity * (1 - discount))
from orders o
join order_details od on o.orderid = od.orderid
where o.customerid = c.customerid
and datepart(yy,o.orderdate) = '1998'
group by o.orderid)
SQL supports few Set operations to be performed on table data. These are used to get meaningful
results from data, under different special conditions. The common set operators are:
• UNION
• INTERSECT
• MINUX/EXCEPT
ID NAME
2 adam
•
•