Supplement: SQL Examples for SQLite Database
The SQLite database is a tiny, lightweight database application, suited for learning SQL and database concepts, or to just explore some database-related ideas without requiring a full-blown database management system (DBMS).
The interested reader is encouraged to work through the SQL commands listed below, in the order in which they are provided. After the initial “Provider” table is created and populated, try imagining what the result of the next SQL statement would be, then enter/paste the statement and compare the output to what you expected. Feel free to explore on your own.
The structure and contents of the initial “Provider” table will look something like this:
ProviderID FirstName LastName HireDate
———- ———- ———- ———-
123456 Ben Spock
123457 Albert Schweitzer 1912-05-09
123458 Derek Shepherd 2005-03-27
123459 Mark Sloan 2005-03-27
You can download the executable free of charge directly from the SQLite project Web site at http://sqlite.org, which also provides extensive documentation and tutorials on its usage.
The lines below that start with two dashes ‘–’ are SQL comments, and thus would not get executed if pasted into the SQLite command window. All other SQL commands must be terminated by a semicolon ‘;’
— set SQLite output format
.header on
.mode column
— data definition, manipulation and initial population
— —————————————————-
CREATE TABLE Provider ( ProviderID char(6) not null, FirstName varchar(24) not null, LastName varchar(64) not null, PRIMARY KEY (ProviderID) );
INSERT INTO Provider ( ProviderID, LastName, FirstName) VALUES ( ‘123456’, ‘Spock’, ‘Benjamin M’ ) ;
— (to see the current structure and contents of the table, use this statement below:)
SELECT * FROM Provider;
ALTER TABLE Provider ADD COLUMN HireDate date;
UPDATE Provider SET FirstName = ‘Ben’ WHERE ProviderID=’123456′;
— further populate table
— ———————-
INSERT INTO Provider ( ProviderID, LastName, FirstName, HireDate) VALUES ( ‘123457’, ‘Schweitzer’, ‘Albert’, ‘1912-05-09’ ) ;
INSERT INTO Provider ( ProviderID, LastName, FirstName, HireDate) VALUES ( ‘123458’, ‘Shepherd‘, ‘Derek’, ‘2005-03-27’ ) ;
INSERT INTO Provider ( ProviderID, LastName, FirstName, HireDate) VALUES ( ‘123459’, ‘Sloan’, ‘Mark’, ‘2005-03-27’ ) ;
— querying
— ——–
SELECT * FROM Provider;
SELECT LastName, HireDate FROM Provider;
SELECT DISTINCT HireDate FROM Provider;
SELECT LastName, FirstName FROM Provider WHERE ProviderID = ‘123456’;
SELECT ProviderID, LastName FROM Provider WHERE HireDate >= ‘2010-01-01’;
SELECT * FROM Provider WHERE HireDate BETWEEN ‘1900-01-01’ AND ‘2000-01-01′;
SELECT ProviderID, LastName FROM Provider WHERE HireDate IS NULL;
— adding a new column ‘Salary’ to the table
ALTER TABLE Provider ADD COLUMN Salary float;
UPDATE Provider SET Salary = 65000 WHERE ProviderID=’123456′;
UPDATE Provider SET Salary = 9500 WHERE ProviderID=’123457′;
UPDATE Provider SET Salary = 142000 WHERE ProviderID=’123458′;
UPDATE Provider SET Salary = 130000 WHERE ProviderID=’123459’;
SELECT * FROM PROVIDER;
— column functions
— —————-
SELECT SUM(Salary) FROM Provider;
SELECT AVG(Salary) FROM Provider;
SELECT MIN(Salary), AVG(Salary), MAX(SALARY) FROM Provider;
SELECT COUNT(*) FROM Provider;
SELECT COUNT(HireDate) FROM Provider;
SELECT COUNT(DISTINCT HireDate) FROM Provider;
— aggregation
— ———–
SELECT HireDate, COUNT(*) FROM PROVIDER GROUP BY HireDate;
SELECT HireDate, COUNT(*) FROM PROVIDER GROUP BY HireDate HAVING COUNT(*) > 1;
— multi-table queries; joining
— —————————-
— create and populate a second table
CREATE TABLE Patient ( PatientID char(6) not null, FirstName varchar(24) not null, LastName varchar(64) not null, DOB date, PrimaryProviderID char(6), PRIMARY KEY (PatientID) );
INSERT INTO Patient VALUES (‘000001’, ‘Brad’, ‘Parker’, ‘1986-03-22’, ‘123458’);
INSERT INTO Patient VALUES (‘000002’, ‘Jennifer’, ‘Miller’, ‘2002-12-09’, ‘123459’);
INSERT INTO Patient VALUES (‘000003’, ‘Olivia’, ‘Silverman’, null, ‘123459’);
INSERT INTO Patient VALUES (‘000004’, ‘John’, ‘Smith’, ‘1955-07-14’, null);
SELECT * FROM Patient;
— multi-table queries
SELECT * FROM Provider, Patient WHERE patient.primaryProviderID=provider.ProviderID;
SELECT Patient.LastName, Patient.FirstName, Patient.DOB, Provider.LastName FROM Provider, Patient WHERE patient.primaryProviderID=provider.ProviderID;
SELECT Patient.LastName, Patient.FirstName, Patient.DOB, Provider.LastName FROM Provider JOIN Patient ON primaryProviderID=ProviderID;