In [79]:
import sqlite3
conn = sqlite3.connect('people.db')
cursor_obj = conn.cursor()
cursor_obj.execute('''
select * from people_main
limit 20;
''')
cursor_obj.fetchall()
Out[79]:
[(1, 'Pheonix Chambers', 'Chefs', 35),
 (2, 'Shaquille Mcintosh', 'Librarians', 29),
 (3, 'Lailanie Mcfarland', 'Principals', 26),
 (4, 'Melana Christian', 'Receptionists', 32),
 (5, 'Nareh Li', 'Security Guard', 36),
 (6, 'Melena Combs', 'Coaches/Instructors', 43),
 (7, 'Armondo Ortega', 'Opticians', 42),
 (8, 'Camryn Hart', 'Executives', 39),
 (9, 'Loren Oneal', 'Physios', 30),
 (10, 'Izeyah Baldwin', 'Chefs', 33),
 (11, 'Joas Thompson', 'Journalists (Not covering war)', 45),
 (12, 'Tayvien Trejo', 'Opticians', 26),
 (13, 'Aamirah Johnston', 'Dentists', 44),
 (14, 'Lewis Erickson', 'Clerks', 41),
 (15, 'Nova Marshall', 'Designers', 29),
 (16, 'Nefertari Bates', 'Insurance Professionals', 33),
 (17, 'Lisbella Lugo', 'Brokers', 36),
 (18, 'Jacqueline Moon', 'Auditors', 45),
 (19, 'Kallan French', 'Deliverymen', 40),
 (20, 'Macklen Ayala', 'Executives', 46)]
In [80]:
# You can get the column names using description
cursor_obj.description
Out[80]:
(('ID', None, None, None, None, None, None),
 ('name', None, None, None, None, None, None),
 ('occupation', None, None, None, None, None, None),
 ('age', None, None, None, None, None, None))
In [81]:
for k in cursor_obj.description:
    print(k[0])
ID
name
occupation
age
In [82]:
cursor_obj.execute('''
select name, age, occupation from people_main
where occupation in ('Librarians', 'Chefs')
order by age desc;
''')
cursor_obj.fetchall()[1:10]
Out[82]:
[('Galen Richardson', 40, 'Librarians'),
 ('Krista Buchanan', 39, 'Chefs'),
 ('Raiza Schmitt', 37, 'Chefs'),
 ('Pheonix Chambers', 35, 'Chefs'),
 ('Izeyah Baldwin', 33, 'Chefs'),
 ('Shaquille Mcintosh', 29, 'Librarians')]
In [83]:
cursor_obj.execute('''
SELECT name FROM sqlite_master WHERE type='table';
''')
cursor_obj.fetchall()
Out[83]:
[('people_main',), ('people_likes',), ('people_friends',)]
In [84]:
cursor_obj.execute('''
SELECT count(ID) FROM people_main;
''')
cursor_obj.fetchall()
Out[84]:
[(100,)]
In [85]:
cursor_obj.execute('''
SELECT count(ID), occupation FROM people_main
GROUP BY occupation;
''')
cursor_obj.fetchall()[:10]
Out[85]:
[(1, 'Agents'),
 (2, 'Architects'),
 (2, 'Artists'),
 (2, 'Auditors'),
 (6, 'Banking Professionals'),
 (2, 'Brokers'),
 (3, 'Car Washers'),
 (1, 'Carpenters'),
 (3, 'Cashiers'),
 (1, 'Caterers')]
In [86]:
cursor_obj.execute('''
SELECT * FROM people_main
JOIN people_likes on people_main.ID = people_likes.ID1
ORDER BY people_main.ID
LIMIT 10;
''')
cursor_obj.fetchall()
Out[86]:
[(1, 'Pheonix Chambers', 'Chefs', 35, 1, 29),
 (4, 'Melana Christian', 'Receptionists', 32, 4, 74),
 (4, 'Melana Christian', 'Receptionists', 32, 4, 100),
 (5, 'Nareh Li', 'Security Guard', 36, 5, 84),
 (7, 'Armondo Ortega', 'Opticians', 42, 7, 26),
 (8, 'Camryn Hart', 'Executives', 39, 8, 59),
 (9, 'Loren Oneal', 'Physios', 30, 9, 28),
 (11, 'Joas Thompson', 'Journalists (Not covering war)', 45, 11, 67),
 (11, 'Joas Thompson', 'Journalists (Not covering war)', 45, 11, 67),
 (11, 'Joas Thompson', 'Journalists (Not covering war)', 45, 11, 41)]
In [87]:
cursor_obj.execute('''
SELECT occupation, count(*) FROM people_main
GROUP BY occupation
having count(*) > 5;
''')
cursor_obj.fetchall()
Out[87]:
[('Banking Professionals', 6)]
In [88]:
# Write a SQL statement that returns the name and number of people that he likes. 
# Results should be ordered by count (descending), and then by name (A-Z). 
cursor_obj.execute('''
select name, count(*) from people_main
join people_likes
on people_main.id = people_likes.id1
group by people_main.id
order by count(*) desc, name;
''')
cursor_obj.fetchall()[:15]
Out[88]:
[('Barnabas Flowers', 4),
 ('Joas Thompson', 4),
 ('Mathius Beard', 4),
 ('Jiaan Bernal', 3),
 ('Kohei Lane', 3),
 ('Lisbella Lugo', 3),
 ('Michel Glass', 3),
 ('Nnamdi Farmer', 3),
 ('Sireen Burch', 3),
 ('Anslee Evans', 2),
 ('Esohe Patel', 2),
 ('Gagan Meza', 2),
 ('Jenabelle Cross', 2),
 ('Lark Parks', 2),
 ('Melana Christian', 2)]
In [90]:
# Write a SQL statement to find pairs (A, B) such that person A and B are friends
cursor_obj.execute('''
with tmp as (
select * from people_main
join people_likes 
on people_main.ID = people_likes.ID1
)
select tmp.name, people_main.name from tmp
join people_main on tmp.ID2 = people_main.ID
''')
cursor_obj.fetchall()[:10]
Out[90]:
[('Armondo Ortega', 'Oakes Kent'),
 ('Oakes Kent', 'Armondo Ortega'),
 ('Kallan French', 'Nael Ryan'),
 ('Barnabas Flowers', 'Nnamdi Farmer'),
 ('Esohe Patel', 'Loyal Wong'),
 ('Oakes Kent', 'Seyla Abbott'),
 ('Sireen Burch', 'Mathius Beard'),
 ('Mathius Beard', 'Sireen Burch'),
 ('Eston Vargas', 'Kohei Lane'),
 ('Kohei Lane', 'Eston Vargas')]
In [91]:
#Write a SQL statement that returns the occupation and and how many people like that occupation. 
#Results should be ordered by popularity from high to low.
cursor_obj.execute('''
select occupation, count(*) as count from people_main
join people_likes
on people_main.id = people_likes.id2
group by people_main.occupation
order by count(*) desc
''')
cursor_obj.fetchall()[:10]
Out[91]:
[('Banking Professionals', 9),
 ('Cashiers', 7),
 ('Car Washers', 6),
 ('Managers', 5),
 ('Executives', 5),
 ('Lawyers', 4),
 ('Journalists (Not covering war)', 4),
 ('IT Professionals', 4),
 ('Security Guard', 3),
 ('Caterers', 3)]
In [93]:
#Write a SQL statement that returns the name and occupation of all people who have more than 3 friends. 
#Results should be ordered by name (A-Z).
cursor_obj.execute('''
select name, occupation, count(*) from people_main
join people_friends
on people_main.id = people_friends.id1
group by people_main.id
having count(*) >= 3;
''')
cursor_obj.fetchall()[:10]
Out[93]:
[('Pheonix Chambers', 'Chefs', 8),
 ('Shaquille Mcintosh', 'Librarians', 8),
 ('Lailanie Mcfarland', 'Principals', 8),
 ('Melana Christian', 'Receptionists', 5),
 ('Nareh Li', 'Security Guard', 4),
 ('Melena Combs', 'Coaches/Instructors', 10),
 ('Armondo Ortega', 'Opticians', 6),
 ('Camryn Hart', 'Executives', 8),
 ('Loren Oneal', 'Physios', 9),
 ('Izeyah Baldwin', 'Chefs', 9)]
In [95]:
#Write a SQL statement that returns the distinct name and age of all people who are liked by anyone younger than them. 
#Results should be ordered by name (A-Z)
cursor_obj.execute('''
with tmp as (
select * from people_main
join people_likes
on people_main.id = people_likes.id1
)
select tmp.name, tmp.age, people_main.name, people_main.age from tmp
join people_main
on tmp.id2 = people_main.id
where tmp.age > people_main.age
''')
cursor_obj.fetchall()[:10]
Out[95]:
[('Armondo Ortega', 42, 'Oakes Kent', 31),
 ('Kallan French', 40, 'Nael Ryan', 35),
 ('Esohe Patel', 30, 'Loyal Wong', 29),
 ('Oakes Kent', 31, 'Seyla Abbott', 25),
 ('Sireen Burch', 32, 'Mathius Beard', 26),
 ('Eston Vargas', 43, 'Kohei Lane', 25),
 ('Izara Roy', 33, 'Sirprince Mejia', 30),
 ('Jenabelle Cross', 46, 'Harlon Norris', 34),
 ('Michel Glass', 35, 'Hidayah Baldwin', 31),
 ('Levana Sloan', 45, 'Rama Herrera', 36)]
In [97]:
#Write a SQL statement to find pairs (A, B) such that person A likes person B, but A is not friends with B. 
#Results should be ordered by ID1 (ascending), then ID2 (ascending).
cursor_obj.execute('''
select distinct people_likes.* from people_likes
left join people_friends
on people_likes.id1 = people_friends.id1
order by people_likes.id1;
''')
cursor_obj.fetchall()[:10]
Out[97]:
[(1, 29),
 (4, 74),
 (4, 100),
 (5, 84),
 (7, 26),
 (8, 59),
 (9, 28),
 (11, 67),
 (11, 41),
 (11, 68)]
In [101]:
# Practice questions
conn = sqlite3.connect('computer.db')
cursor_obj = conn.cursor()
In [102]:
cursor_obj.execute('''
CREATE TABLE Manufacturers (
	Code INTEGER PRIMARY KEY NOT NULL,
	Name CHAR(50) NOT NULL 
);
''')
cursor_obj.execute('''
CREATE TABLE Products (
	Code INTEGER PRIMARY KEY NOT NULL,
	Name CHAR(50) NOT NULL ,
	Price REAL NOT NULL ,
	Manufacturer INTEGER NOT NULL 
		CONSTRAINT fk_Manufacturers_Code REFERENCES Manufacturers(Code)
);
''')
Out[102]:
<sqlite3.Cursor at 0x121727ea0>
In [103]:
cursor_obj.execute('''
INSERT INTO Manufacturers(Code,Name) VALUES (1,'Sony'),
                                    (2,'Creative Labs'),
                                    (3,'Hewlett-Packard'),
                                    (4,'Iomega'),
                                    (5,'Fujitsu'),
                                    (6,'Winchester');   
''')
Out[103]:
<sqlite3.Cursor at 0x121727ea0>
In [104]:
cursor_obj.execute('''
INSERT INTO Products(Code,Name,Price,Manufacturer) VALUES
(1,'Hard drive',240,5),
(2,'Memory',120,6),
(3,'ZIP drive',150,4),
(4,'Floppy disk',5,6),
(5,'Monitor',240,1),
(6,'DVD drive',180,2),
(7,'CD drive',90,2),
(8,'Printer',270,3),
(9,'Toner cartridge',66,3),
(10,'DVD burner',180,2);
''')
Out[104]:
<sqlite3.Cursor at 0x121727ea0>
In [105]:
cursor_obj.execute('''
select * from Products;
''')
cursor_obj.fetchall()
Out[105]:
[(1, 'Hard drive', 240.0, 5),
 (2, 'Memory', 120.0, 6),
 (3, 'ZIP drive', 150.0, 4),
 (4, 'Floppy disk', 5.0, 6),
 (5, 'Monitor', 240.0, 1),
 (6, 'DVD drive', 180.0, 2),
 (7, 'CD drive', 90.0, 2),
 (8, 'Printer', 270.0, 3),
 (9, 'Toner cartridge', 66.0, 3),
 (10, 'DVD burner', 180.0, 2)]
In [106]:
# 1. Select the names and the prices of all the products in the store.
cursor_obj.execute('''
select name, price from products;
''')
cursor_obj.fetchall()
Out[106]:
[('Hard drive', 240.0),
 ('Memory', 120.0),
 ('ZIP drive', 150.0),
 ('Floppy disk', 5.0),
 ('Monitor', 240.0),
 ('DVD drive', 180.0),
 ('CD drive', 90.0),
 ('Printer', 270.0),
 ('Toner cartridge', 66.0),
 ('DVD burner', 180.0)]
In [107]:
# 2. Select the name of the products with a price less than or equal to $200.
cursor_obj.execute('''
select name from products
where price <= 200;
''')
cursor_obj.fetchall()
Out[107]:
[('Memory',),
 ('ZIP drive',),
 ('Floppy disk',),
 ('DVD drive',),
 ('CD drive',),
 ('Toner cartridge',),
 ('DVD burner',)]
In [108]:
# 3. Select the name and price in cents (i.e., the price must be multiplied by 100).
cursor_obj.execute('''
select name, price*100 from products;
''')
cursor_obj.fetchall()
Out[108]:
[('Hard drive', 24000.0),
 ('Memory', 12000.0),
 ('ZIP drive', 15000.0),
 ('Floppy disk', 500.0),
 ('Monitor', 24000.0),
 ('DVD drive', 18000.0),
 ('CD drive', 9000.0),
 ('Printer', 27000.0),
 ('Toner cartridge', 6600.0),
 ('DVD burner', 18000.0)]
In [109]:
# 4. Compute the average price of all the products.
cursor_obj.execute('''
select avg(price) from products;
''')
cursor_obj.fetchall()
Out[109]:
[(154.1,)]
In [110]:
# 5. Select the name and price of all products with a price larger than or equal to $180, and sort first by price 
# (in descending order), and then by name (in ascending order).
cursor_obj.execute('''
select name, price from products
where price >= 180
order by price desc, name;
''')
cursor_obj.fetchall()
Out[110]:
[('Printer', 270.0),
 ('Hard drive', 240.0),
 ('Monitor', 240.0),
 ('DVD burner', 180.0),
 ('DVD drive', 180.0)]
In [111]:
# 6. Select the name and price of the cheapest product.
cursor_obj.execute('''
SELECT name,price
  FROM Products
  ORDER BY price ASC
  LIMIT 1
  ''')
cursor_obj.fetchall()
Out[111]:
[('Floppy disk', 5.0)]
In [112]:
# 7. Select the average price of each manufacturer's products, showing the manufacturer's name.
cursor_obj.execute('''
SELECT avg(products.price), manufacturers.name from Manufacturers
join products on manufacturers.code = products.code
group by Manufacturers.name
  ''')
cursor_obj.fetchall()
Out[112]:
[(120.0, 'Creative Labs'),
 (240.0, 'Fujitsu'),
 (150.0, 'Hewlett-Packard'),
 (5.0, 'Iomega'),
 (240.0, 'Sony'),
 (180.0, 'Winchester')]
In [ ]: