Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

SQL Tutorial

Database Tables:
Customers
CREATE TABLE `Customers` (
`ID` int(11) NOT NULL auto_increment,
`Name` varchar(255) NOT NULL default '',
`Address` varchar(255) default NULL,
`City` varchar(255) default NULL,
`State` varchar(45) default NULL,
`Zip` varchar(45) default NULL,
`Country` varchar(255) default NULL,
`Phone` varchar(45) default NULL,
PRIMARY KEY (`ID`)
);

Contacts
CREATE TABLE `Contacts` (
`ID` int(11) NOT NULL auto_increment,
`CustomerID` int(11) default NULL,
`Firstname` varchar(45) NOT NULL default '',
`Lastname` varchar(45) NOT NULL default '',
`Title` varchar(45) default NULL,
`Phone` varchar(45) default NULL,
`Email` varchar(255) default NULL,
PRIMARY KEY (`ID`),
KEY `FK_contacts_1` (`CustomerID`),
CONSTRAINT `FK_contacts_1` FOREIGN KEY (`CustomerID`) REFERENCES
`Customers` (`ID`)
);


Shifts
CREATE TABLE `Shifts` (
`ID` int(11) NOT NULL auto_increment,
`Name` varchar(255) NOT NULL default '',
`StartHour` int(11) NOT NULL default '0',
`StartMin` int(11) NOT NULL default '0',
`EndHour` int(11) NOT NULL default '0',
`EndMin` int(11) NOT NULL default '0',
`Enabled` int(11) NOT NULL default '1',
PRIMARY KEY (`ID`)
);

Equipment
CREATE TABLE `Equipment` (
`ID` int(11) NOT NULL auto_increment,
`Name` varchar(45) NOT NULL default '',
`Description` text,
PRIMARY KEY (`ID`)
);

Causes
CREATE TABLE `Causes` (
`ID` int(11) NOT NULL auto_increment,
`EquipID` int(11) NOT NULL default '0',
`Name` varchar(45) NOT NULL default '',
`Description` text,
PRIMARY KEY (`ID`),
KEY `Index_2` (`EquipID`),
CONSTRAINT `FK_causes_1` FOREIGN KEY (`EquipID`) REFERENCES
`Equipment` (`ID`)
);

Downtime
CREATE TABLE `Downtime` (
`ID` int(11) NOT NULL auto_increment,
`CauseID` int(11) NOT NULL default '0',
`Duration` int(11) NOT NULL default '0',
`t_stamp` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`ID`),
KEY `Index_2` (`CauseID`),
CONSTRAINT `FK_downtime_1` FOREIGN KEY (`CauseID`) REFERENCES
`Causes` (`ID`)
);


Recipes
CREATE TABLE `recipes` (
`ID` int(11) NOT NULL auto_increment,
`Name` varchar(45) NOT NULL default '',
`SP1` float NOT NULL default '0',
`SP2` float NOT NULL default '0',
`SP3` float NOT NULL default '0',
`SP4` float NOT NULL default '0',
`SP5` float NOT NULL default '0',
`Use` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`ID`)
);

SQL Query Practice


Using the SELECT statement
1) Show all information of all customers

(122 rows, showing first 10 records)


2) Show the name and address of all customers ordered by the name in ascending order

(122 rows, showing first 10 records)

3) Show the name and state of all customers in the state of CA

(11 rows)
4) Show the name, city and phone number of all customers in the city of Las Vegas or
San Francisco or Pasadena

(4 rows)
5) Show the number of downtime entries from January 1, 2009 January 2, 2009

6) Show the cause name, timestamp and duration of each downtime occurrence ordered
by the timestamp in ascending order

(19 rows)

7) Show the equipment name, number of downtime events and downtime duration for
each piece of equipment ordered by the equipment name in ascending order

(4 rows)
8) Show the cause name, duration and timestamp of all downtime events for the
Conveyor Line piece of equipment (hint: equipment ID is 1) from January 1, 2009
January 2, 2009 ordered by the timestamp in ascending order

(4 rows)

9) Show the equipment name, cause name, duration and timestamp for each downtime
occurrence between January 1, 2009 January 2, 2009 ordered by equipment then
timestamp in ascending order

(6 rows)
10) Show the equipment name, cause name, number of downtime events and downtime
duration for each cause ordered by the equipment name then cause name in ascending
order

(9 rows)

Query Answers
1) SELECT * FROM Customers
2) SELECT Name, Address FROM Customers ORDER BY Name ASC
3) SELECT Name, State FROM Customers WHERE State = 'CA'
4) SELECT Name, City, Phone FROM Customers WHERE City = 'Las Vegas' OR City =
'San Francisco' OR City = 'Pasadena'
5) SELECT COUNT(ID) FROM Downtime WHERE t_stamp >= '2009-01-01 00:00:00'
AND t_stamp <= '2009-01-02 23:59:59'
6) SELECT c.Name, d.Duration, d.t_stamp FROM Downtime d JOIN Causes c ON c.ID
= d.CauseID ORDER BY t_stamp ASC
7) SELECT e.Name, COUNT(d.ID), SUM(d.Duration) FROM Downtime d JOIN Causes
c ON c.ID = d.CauseID JOIN Equipment e ON e.ID = c.EquipID GROUP BY e.ID
ORDER BY e.Name ASC
8) SELECT c.Name, d.Duration, d.t_stamp FROM Downtime d JOIN Causes c ON c.ID
= d.CauseID WHERE c.EquipID = 1 AND t_stamp >= '2009-01-01 00:00:00' AND
t_stamp <= '2009-01-02 23:59:59' ORDER BY d.t_stamp ASC
OR
SELECT c.Name, d.Duration, d.t_stamp FROM Downtime d JOIN Causes c ON c.ID =
d.CauseID JOIN Equipment e ON e.ID = c.EquipID WHERE e.Name = 'Conveyor Line'
AND t_stamp >= '2009-01-01 00:00:00' AND t_stamp <= '2009-01-02 23:59:59' ORDER
BY d.t_stamp ASC
9) SELECT e.Name, c.Name, d.Duration, d.t_stamp FROM Downtime d JOIN Causes c
ON c.ID = d.CauseID JOIN Equipment e ON e.ID = c.EquipID WHERE t_stamp >=
'2009-01-01 00:00:00' AND t_stamp <= '2009-01-02 23:59:59' ORDER BY e.Name
ASC, d.t_stamp ASC
10) SELECT e.Name, c.Name, COUNT(d.ID), SUM(d.Duration) FROM Downtime d
JOIN Causes c ON c.ID = d.CauseID JOIN Equipment e ON e.ID = c.EquipID GROUP
BY c.ID ORDER BY e.Name, c.Name ASC

You might also like