0% found this document useful (0 votes)
97 views97 pages

Database Essentials 18S PDF

The document provides information about database essentials including database concepts like tables, records, fields, primary keys, relationships, and SQL queries. It includes examples of creating and modifying database tables, inserting and deleting records, joining tables, aggregating data using functions like COUNT, SUM, and AVG, filtering using WHERE clauses, sorting using ORDER BY, and more. It also covers SQL set operations like UNION, INTERSECT, and EXCEPT.

Uploaded by

Nikolay Semko
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)
97 views97 pages

Database Essentials 18S PDF

The document provides information about database essentials including database concepts like tables, records, fields, primary keys, relationships, and SQL queries. It includes examples of creating and modifying database tables, inserting and deleting records, joining tables, aggregating data using functions like COUNT, SUM, and AVG, filtering using WHERE clauses, sorting using ORDER BY, and more. It also covers SQL set operations like UNION, INTERSECT, and EXCEPT.

Uploaded by

Nikolay Semko
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/ 97

DATABASE ESSENTIALS








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');

SELECT id, name FROM users WHERE join_date = '2013-12-29';

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;


CREATE TABLE login


(
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
full_name VARCHAR(100),
user_type_id INT REFERENCES user_types(user_type_id)
);


ALTER TABLE login RENAME TO employees;


ALTER TABLE employees ADD COLUMN last_login datetime;
ALTER TABLE employees CHANGE COLUMN full_name employee_name
VARCHAR(100);
ALTER TABLE employees CHANGE COLUMN employee_name employee_name
VARCHAR(150);
ALTER TABLE employees CHANGE COLUMN employee_name fill_name
VARCHAR(100);
ALTER TABLE employees DROP COLUMN last_login;

DROP employees;
SELECT * FROM account_type;
INSERT INTO employees
(
username,
password,
full_name
)
VALUES
(
‘johnd’,
‘mypass’,
‘John Doe’
);

UPDATE accounts SET phone='615551212'


WHERE email='[email protected]';

UPDATE accounts
SET phone='615551234',
fullname='Dan G'
WHERE phone='615551212';

DELETE from account_type where description='user';


SELECT description FROM account_type;

SELECT * from employees where full_name='administrator';


SELECT last_login FROM employees WHERE username=’johnd’;
SELECT username FROM employees where last_login >= ‘Oct 1, 2007’;












SELECT column AS column_alias FROM table


SELECT column FROM table AS table_alias

SELECT last_user_login_timestamp, username FROM user_login_logs

SELECT last_user_login_timestamp as last_login, username FROM


user_login_logs as ull WHERE ull.last_user_login_timestamp >=
‘01/01/2008’

SELECT field1[,field2][,field3…] FROM <TABLENAME>


[WHERE Clause]
ORDER BY ordering_field [ASC/DESC], [ordering_field2 [ASC/DESC]];

SELECT username,last_login_timestamp, first_name, last_name FROM


employee
ORDER by last_login desc,last_name asc;
SELECT SUM(quantity) AS Total
FROM products

Total
-----------
1837
SELECT AVG(unit_price * quantity) As AveragePrice
FROM products
WHERE continent = “North America”

AveragePrice
---------------------
862.3075

SELECT COUNT(*) AS 'Number of Large Orders'


FROM products
WHERE quantity > 100

Number of Large Orders


----------------------
3
SELECT COUNT(ALL continent) As 'Number of continents'
FROM products

Number of continents
--------------------
7

SELECT COUNT(DISTINCT continent) As 'Number of continents'


FROM products

Number of continents
--------------------
3

SELECT MAX(quantity * unit_price)As 'Largest Order'


FROM products

Largest Order
---------------------
2517.58
SELECT continent, MIN(quantity * unit_price) AS 'Smallest Order'
FROM products
GROUP BY continent

continent Smallest Order


------------- ---------------------
Africa 167.04
Europe 2099.02
North America 70.65
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;

+----+--------+-----+---------+---------+
| 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

SELECT * FROM TableA


INNER JOIN TableB
ON TableA.name = TableB.name

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

SELECT * FROM TableA


LEFT 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

SELECT * FROM TableA


LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS 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 distinct country


from customers
where country not in (select distinct country
from suppliers)
select country,
companyname,
contactname,
contacttitle,
phone
from customers
where country = (select top 1 country
from customers c
join orders o
on c.customerid = o.customerid
group by country
order by count(*))

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

The examples will use the following data:

Union SQL query will be,

select * from First


UNION
select * from second
Union All query will be like,

select * from First


UNION ALL
select * from second

The result table will look like,


The First table,

Intersect query will be,

select * from First


INTERSECT
select * from second

The result table will look like

ID NAME
2 adam

Minus query will be,

select * from First


EXCEPT
select * from second

The result table will look like,





You might also like