Lecture 5 Getting Started With SQL - Using SQLite
Lecture 5 Getting Started With SQL - Using SQLite
By Wycliffe Mwebi
Lecture 5:
Getting Started with SQL -using SQLite
What is SQLite
SQLite is a software library that provides a relational database management system. The lite in SQLite
means lightweight in terms of setup, database administration, and required resources.
Server-less
Normally, an RDBMS such as MySQL, PostgreSQL, etc., requires a separate server process to operate.
The applications that want to access the database server use TCP/IP protocol to send and receive
requests. This is called client/server architecture.
SQLite database is integrated with the application that accesses the database. The applications interact
with the SQLite database read and write directly from the database files stored on disk.
Self-Contained
SQLite is self-contained means it requires minimal support from the operating system or external library.
This makes SQLite usable in any environment especially in embedded devices like iPhones, Android
phones, game consoles, handheld media players, etc.
SQLite is developed using ANSI-C. The source code is available as a big sqlite3.c and its header file
sqlite3.h. If you want to develop an application that uses SQLite, you just need to drop these files into
your project and compile it with your code.
Zero-configuration
Because of the serverless architecture, you don’t need to “install” SQLite before using it. There is no
server process that needs to be configured, started, and stopped.
Transactional
All transactions in SQLite are fully ACID-compliant. It means all queries and changes are Atomic,
Consistent, Isolated, and Durable.
In other words, all changes within a transaction take place completely or not at all even when an
unexpected situation like application crash, power failure, or operating system crash occurs.
SQLite allows a single database connection to access multiple database files simultaneously. This brings
many nice features like joining tables in different databases or copying data between databases in a
single command.
SQLite is capable of creating in-memory databases that are very fast to work with.
Database Systems Lecture notes
By Wycliffe Mwebi
SQLite provides various tools for working across platforms e.g., Windows, Linux, and Mac. You need to
select an appropriate version to download.
For example, to work with SQLite on Windows, you download the command-line shell program as
shown in the screenshot below.
The downloaded file is in the ZIP format and its size is quite small.
2. Second, extract the content of the file that you downloaded in the previous section to
the C:\sqlite folder. You should see three programs in the C:\sqlite folder as shown below:
Second, type sqlite3 and press enter, you should see the following output:
Third, you can type the .help command from the sqlite> prompt to see all available commands in sqlite3.
However, sometimes, you may want to work with the SQLite databases using an intuitive GUI tool.
There are many GUI tools for managing SQLite databases available ranging from freeware to commercial
licenses.
SQLiteStudio
The SQLiteStudio tool is a free GUI tool for managing SQLite databases. It is free, portable, intuitive, and
cross-platform. SQLite tool also provides some of the most important features to work with SQLite
databases such as importing, exporting data in various formats including CSV, XML, and JSON.
You can download the SQLiteStudio installer or its portable version by visiting the download page. Then,
you can extract (or install) the download file to a folder e.g., C:\sqlite\gui\ and launch it.
DBeaver is another free multi-platform database tool. It supports all popular major relational
database systems MySQL, PostgreSQL, Oracle, DB2, SQL Server, Sybase.. including SQLite.
DB Browser for SQLite – is an open-source tool to manage database files compatible with
SQLite.
The following database diagram illustrates the chinook database tables and their relationships.
Database Systems Lecture notes
By Wycliffe Mwebi
4. On the File input textbox, click on the green (+) sign and select the chinook database you
downloaded.
7. You should now see your chinook database on SQLite Studio as shown below;
8. Double click on the database name –Chinook to reveal all the tables within
Let’s say for example, you want to views all the records in the employees table.
SELECT *
FROM employees
Hit the execute query button
Database Systems Lecture notes
By Wycliffe Mwebi
SELECT name
FROM artists
2. Print all data from the table Invoice where BillingCountry is Germany
SELECT *
FROM invoices
WHERE BillingCountry = 'Germany'
3. Count how many albums are in the database. Hint: look for Min&Max and Count, Avg, Sum.
SELECT COUNT(*)
FROM albums
SELECT COUNT(*)
FROM Customers
WHERE Country = 'France'
SELECT *
FROM tracks WHERE UnitPrice > 0.99;
6. Which tracks are less expensive than $0.99? (Are there any?)
SELECT *
FROM tracks WHERE UnitPrice < 0.99;
SELECT *
FROM tracks WHERE Name like "%love%";
select *
FROM tracks WHERE Name like "love%";
10. Provide a query showing Customers (just their full names, customer ID and country) who are not in
the US
SELECT customerid, firstname, lastname, country
FROM customers
WHERE not country = 'USA'
12. Provide a query showing the Invoices of customers who are from Brazil. The resultant table should
show the customer's full name, Invoice ID, Date of the invoice and billing country.
13. Provide a query showing only the Employees who are Sales Agents.
SELECT * from employees
WHERE employees.title = 'Sales Support Agent';
14. Provide a query showing a unique list of billing countries from the Invoice table.
SELECT distinct billingcountry
FROM invoices;
15. Provide a query showing the invoices of customers who are from Brazil.
SELECT *
FROM customers as c, invoices as i
WHERE c.country = 'Brazil' and
c.customerid = i.customerid;
16. Provide a query that shows the invoices associated with each sales agent. The resultant table should
include the Sales Agent's full name.
SELECT
e.FirstName,
e.LastName,
e.Title,
i.*
FROM Invoices i
JOIN Customers c ON c.CustomerId = i.CustomerId
Database Systems Lecture notes
By Wycliffe Mwebi
17. Provide a query that shows the Invoice Total, Customer name, Country and Sale Agent name for all
invoices and customers.
SELECT e.firstname as 'employee first',
e.lastname as 'employee last',
c.firstname as 'customer first',
c.lastname as 'customer last',
c.country,
i.total
FROM employees as e
join customers as c on e.employeeid = c.supportrepid
join invoices as i on c.customerid = i.customerid
18. How many Invoices were there in 2009 and 2011? What are the respective total sales for each of
those years?
SELECT count(i.invoiceid), sum(i.total)
FROM invoices as i
WHERE i.invoicedate between datetime('2011-01-01 00:00:00') and datetime('2011-12-31
00:00:00');
19. Looking at the Invoice_items table, provide a query that COUNTs the number of line items for
Invoice ID 37.
SELECT count(i.invoiceid)
FROM invoice_items as i
WHERE i.invoiceid = 37
20. Looking at the Invoice_items table, provide a query that COUNTs the number of line items for each
Invoice. HINT: [GROUP BY]
SELECT invoiceid, count(invoice_items)
FROM invoice_items
GROUP by invoiceid
21. Provide a query that includes the track name with each invoice line item.
SELECT i.*, t.name
FROM invoice_items as i, tracks as t
on i.trackid = t.trackid
Database Systems Lecture notes
By Wycliffe Mwebi
22. Provide a query that includes the purchased track name AND artist name with each invoice line
item.
SELECT i.*, t.name as 'track', ar.name as 'artist'
from invoice_items as i
join tracks as t on i.trackid = t.trackid
join albums as al on al.albumid = t.albumid
join artists as ar on ar.artistid = al.artistid
23. Provide a query that shows the # of invoices per country. HINT: [GROUP BY]
SELECT billingcountry, count(billingcountry) as '# of invoices'
FROM invoices
GROUP by billingcountry
24. Provide a query that shows the total number of tracks in each playlist. The Playlist name should be
included on the resultant table.
SELECT *, count(trackid) as '# of tracks'
FROM playlist_track, playlists
on playlist_track.playlistid = playlists.playlistid
GROUP BY playlists.playlistid
25. Provide a query that shows all the Tracks, but displays no IDs. The resultant table should include the
Album name, Media type and Genre.
SELECT t.name as 'track', t.composer, t.milliseconds, t.bytes, t.unitprice, a.title as 'album',
g.name as 'genre', m.name as 'media type'
FROM tracks as t
join albums as a on a.albumid = t.albumid
join genres as g on g.genreid = t.genreid
join media_types as m on m.mediatypeid = t.mediatypeid
26. Provide a query that shows all Invoices but includes the # of invoice items.
27. Provide a query that shows total sales made by each sales agent.
SELECT e.*, count(i.invoiceid) as 'Total Number of Sales'
FROM employees as e
JOIN customers as c on e.employeeid = c.supportrepid
join invoices as i on i.customerid = c.customerid
GROUP BY e.employeeid
Database Systems Lecture notes
By Wycliffe Mwebi
SELECT *, max(total)
FROM
(select e.*, sum(total) as 'Total'
FROM employees as e
30. Which sales agent made the most in sales over all?
31. Provide a query that shows the # of customers assigned to each sales agent.
32. Provide a query that shows the total sales per country. Which country's customers spent the most?
33. Provide a query that shows the most purchased track of 2013.
34. Provide a query that shows the top 5 most purchased tracks over all.
35. Provide a query that shows the top 3 bestselling artists.
36. Which albums are by Aerosmith?
SELECT Name
FROM Artists
JOIN Albums on Albums.ArtistId = Artists.ArtistId
WHERE Title = "Facelift";
SELECT Name
FROM Tracks
JOIN Albums on Tracks.AlbumId = Albums.AlbumId
WHERE Albums.Title = "Frank";
43. Which tracks are on the playlist "Brazilian Music" and who are they by?
SELECT count(*)
FROM Tracks
JOIN Albums on Tracks.AlbumId = Albums.AlbumId
WHERE Title = "Facelift";
SELECT count(*)
FROM Artists
JOIN Albums on Albums.ArtistId = Artists.ArtistId
WHERE Name = "Amy Winehouse";
SELECT SUM(UnitPrice)
FROM Tracks;
49. How many tracks are there by each of Amy Winehouse's albums?
51. How many songs are on each playlist? Order by the number of songs on each playlist, most to least.
52. How many albums are there for each artist? Arrange by number of albums in descending order and I
only want the top 5.
SELECT Artists.Name, count(*)
FROM Artists
JOIN Albums on Albums.ArtistId = Artists.ArtistId
GROUP BY Artists.Name
ORDER BY count(*) desc
limit 10;
53. What are the top 5 longest albums and who are they by?
SELECT Albums.Title, Artists.Name, sum(Milliseconds)
FROM Tracks
JOIN Albums on Tracks.AlbumId = Albums.AlbumId
JOIN Artists on Albums.ArtistId = Artists.ArtistId
GROUP BY Tracks.AlbumId
ORDER BY sum(Milliseconds) desc
limit 5;
Database Systems Lecture notes
By Wycliffe Mwebi
Exercises
Attempt the following exercises;