├── 6. Scientists ├── README.md ├── Scientists.sql ├── AssignedTo.sql └── Projects.sql ├── 7. Planet Express ├── README.md ├── Client.sql ├── Planet.sql ├── Has_Clearance.sql ├── Shipment.sql ├── Employee.sql └── Package.sql ├── 5. Pieces and providers ├── Pieces.sql ├── Providers.sql ├── Provides.sql └── README.md ├── 3. The warehouse ├── Warehouses.sql ├── Boxes.sql └── README.md ├── 2. Employee management ├── Departments.sql ├── Employees.sql └── README.md ├── 4. Movie theatres ├── MovieTheaters.sql ├── Movies.sql └── README.md ├── README.md ├── 1. The computer store ├── build_schema.sql └── README.md └── 8. The Hospital ├── README.md └── build_schema.sql /6. Scientists/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 1. List all the scientists' names, their projects' names, and the hours worked by that scientist on each project, in alphabetical order of project name, then scientist name. 4 | 5 | ```sql 6 | SELECT S.Name AS ScientistName, P.Name AS ProjectName, P.Hours AS Hours 7 | FROM AssignTo AS AT 8 | INNER JOIN Scientists AS S ON S.SSN = AT.Scientist 9 | INNER JOIN Projects AS P ON P.Code = AT.Project 10 | ORDER BY P.Name ASC, S.Name ASC 11 | ; 12 | ``` 13 | -------------------------------------------------------------------------------- /7. Planet Express/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | The first several exercises build on each other and illustrate the use of subqueries. 4 | 5 | 1. Who received a 1.5kg package? *(Here I assume that `Weight` is measured using `kg`).* 6 | 7 | ```sql 8 | SELECT C.Name 9 | FROM Client AS C 10 | JOIN Package AS P 11 | ON C.AccountNumber = P.Recipient 12 | WHERE P.weight = 1.5 13 | ; 14 | ``` 15 | 16 | 2. What is the total weight of all the packages that he sent? 17 | 18 | > *What is this question asking?* 19 | 20 | ```sql 21 | 22 | ``` 23 | 24 | 3. Which pilots transported those packages? 25 | 26 | > *Also this one...* 27 | 28 | ```sql 29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /5. Pieces and providers/Pieces.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:36:27 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Pieces` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Pieces`; 24 | CREATE TABLE `Pieces` ( 25 | `Code` int(11) NOT NULL, 26 | `Name` text NOT NULL, 27 | PRIMARY KEY (`Code`) 28 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 29 | 30 | -- ---------------------------- 31 | -- Records of `Pieces` 32 | -- ---------------------------- 33 | BEGIN; 34 | INSERT INTO `Pieces` VALUES ('1', 'Sprocket'), ('2', 'Screw'), ('3', 'Nut'), ('4', 'Bolt'); 35 | COMMIT; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /5. Pieces and providers/Providers.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:36:33 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Providers` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Providers`; 24 | CREATE TABLE `Providers` ( 25 | `Code` varchar(40) NOT NULL, 26 | `Name` text NOT NULL, 27 | PRIMARY KEY (`Code`) 28 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 29 | 30 | -- ---------------------------- 31 | -- Records of `Providers` 32 | -- ---------------------------- 33 | BEGIN; 34 | INSERT INTO `Providers` VALUES ('HAL', 'Clarke Enterprises'), ('RBT', 'Susan Calvin Corp.'), ('TNBC', 'Skellington Supplies'); 35 | COMMIT; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /3. The warehouse/Warehouses.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:31:51 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Warehouses` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Warehouses`; 24 | CREATE TABLE `Warehouses` ( 25 | `Code` int(11) NOT NULL, 26 | `Location` varchar(255) NOT NULL, 27 | `Capacity` int(11) NOT NULL, 28 | PRIMARY KEY (`Code`) 29 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 30 | 31 | -- ---------------------------- 32 | -- Records of `Warehouses` 33 | -- ---------------------------- 34 | BEGIN; 35 | INSERT INTO `Warehouses` VALUES ('1', 'Chicago', '3'), ('2', 'Chicago', '4'), ('3', 'New York', '7'), ('4', 'Los Angeles', '2'), ('5', 'San Francisco', '8'); 36 | COMMIT; 37 | 38 | SET FOREIGN_KEY_CHECKS = 1; 39 | -------------------------------------------------------------------------------- /2. Employee management/Departments.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 05/22/2019 15:27:57 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Departments` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Departments`; 24 | CREATE TABLE `Departments` ( 25 | `Code` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | `Budget` decimal(10,0) NOT NULL, 28 | PRIMARY KEY (`Code`) 29 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 30 | 31 | -- ---------------------------- 32 | -- Records of `Departments` 33 | -- ---------------------------- 34 | BEGIN; 35 | INSERT INTO `Departments` VALUES ('14', 'IT', '65000'), ('37', 'Accounting', '15000'), ('59', 'Human Resources', '240000'), ('77', 'Research', '55000'); 36 | COMMIT; 37 | 38 | SET FOREIGN_KEY_CHECKS = 1; 39 | -------------------------------------------------------------------------------- /7. Planet Express/Client.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:41:23 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Client` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Client`; 24 | CREATE TABLE `Client` ( 25 | `AccountNumber` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | PRIMARY KEY (`AccountNumber`) 28 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 29 | 30 | -- ---------------------------- 31 | -- Records of `Client` 32 | -- ---------------------------- 33 | BEGIN; 34 | INSERT INTO `Client` VALUES ('1', 'Zapp Brannigan'), ('2', 'Al Gore\'s Head'), ('3', 'Barbados Slim'), ('4', 'Ogden Wernstrom'), ('5', 'Leo Wong'), ('6', 'Lrrr'), ('7', 'John Zoidberg'), ('8', 'John Zoidfarb'), ('9', 'Morbo'), ('10', 'Judge John Whitey'), ('11', 'Calculon'); 35 | COMMIT; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /4. Movie theatres/MovieTheaters.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:33:48 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `MovieTheaters` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `MovieTheaters`; 24 | CREATE TABLE `MovieTheaters` ( 25 | `Code` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | `Movie` int(11) DEFAULT NULL, 28 | PRIMARY KEY (`Code`), 29 | KEY `Movie` (`Movie`), 30 | CONSTRAINT `movietheaters_ibfk_1` FOREIGN KEY (`Movie`) REFERENCES `Movies` (`Code`) 31 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 32 | 33 | -- ---------------------------- 34 | -- Records of `MovieTheaters` 35 | -- ---------------------------- 36 | BEGIN; 37 | INSERT INTO `MovieTheaters` VALUES ('1', 'Odeon', '5'), ('2', 'Imperial', '1'), ('3', 'Majestic', null), ('4', 'Royale', '6'), ('5', 'Paraiso', '3'), ('6', 'Nickelodeon', null); 38 | COMMIT; 39 | 40 | SET FOREIGN_KEY_CHECKS = 1; 41 | -------------------------------------------------------------------------------- /4. Movie theatres/Movies.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:33:54 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Movies` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Movies`; 24 | CREATE TABLE `Movies` ( 25 | `Code` int(11) NOT NULL, 26 | `Title` varchar(255) NOT NULL, 27 | `Rating` varchar(255) DEFAULT NULL, 28 | PRIMARY KEY (`Code`) 29 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 30 | 31 | -- ---------------------------- 32 | -- Records of `Movies` 33 | -- ---------------------------- 34 | BEGIN; 35 | INSERT INTO `Movies` VALUES ('1', 'Citizen Kane', 'PG'), ('2', 'Singin\' in the Rain', 'G'), ('3', 'The Wizard of Oz', 'G'), ('4', 'The Quiet Man', null), ('5', 'North by Northwest', null), ('6', 'The Last Tango in Paris', 'NC-17'), ('7', 'Some Like it Hot', 'PG-13'), ('8', 'A Night at the Opera', null), ('9', 'Citizen King', 'G'); 36 | COMMIT; 37 | 38 | SET FOREIGN_KEY_CHECKS = 1; 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-Exercises 2 | 3 | > *The purpose of any exercise is to steadily develop skills and to acquire the automatic algorithms for fulfilling certain operations. As for the SQL language, practical exercises are intended to enable the database programmer to quickly devise SQL queries in order to solve practically any problem, by having already studied similar problems in exercises. This book provides such a collection of practical solved SQL exercises.* ---- *From Wiki Book SQL-Exercises* 4 | 5 | ## Exercises 6 | 7 | - [x] [The computer store](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/1.%20The%20computer%20store) 8 | - [x] [Employee management](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/2.%20Employee%20management) 9 | - [x] [The warehouse](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/3.%20The%20warehouse) 10 | - [x] [Movie theatres](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/4.%20Movie%20theatres) 11 | - [x] [Pieces and providers](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/5.%20Pieces%20and%20providers) 12 | - [x] [Scientists](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/6.%20Scientists) 13 | - [x] [Planet Express](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/7.%20Planet%20Express) 14 | - [x] [The Hospital](https://github.com/Zhenye-Na/SQL-Exercises/tree/master/8.%20The%20Hospital) -------------------------------------------------------------------------------- /6. Scientists/Scientists.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:38:03 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Scientists` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Scientists`; 24 | CREATE TABLE `Scientists` ( 25 | `SSN` int(11) NOT NULL, 26 | `Name` char(30) NOT NULL, 27 | PRIMARY KEY (`SSN`) 28 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 29 | 30 | -- ---------------------------- 31 | -- Records of `Scientists` 32 | -- ---------------------------- 33 | BEGIN; 34 | INSERT INTO `Scientists` VALUES ('123234877', 'Michael Rogers'), ('152934485', 'Anand Manikutty'), ('222364883', 'Carol Smith'), ('326587417', 'Joe Stevens'), ('332154719', 'Mary-Anne Foster'), ('332569843', 'George ODonnell'), ('546523478', 'John Doe'), ('631231482', 'David Smith'), ('654873219', 'Zacary Efron'), ('745685214', 'Eric Goldsmith'), ('845657245', 'Elizabeth Doe'), ('845657246', 'Kumar Swamy'); 35 | COMMIT; 36 | 37 | SET FOREIGN_KEY_CHECKS = 1; 38 | -------------------------------------------------------------------------------- /7. Planet Express/Planet.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:40:58 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Planet` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Planet`; 24 | CREATE TABLE `Planet` ( 25 | `PlanetID` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | `Coordinates` double NOT NULL, 28 | PRIMARY KEY (`PlanetID`) 29 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 30 | 31 | -- ---------------------------- 32 | -- Records of `Planet` 33 | -- ---------------------------- 34 | BEGIN; 35 | INSERT INTO `Planet` VALUES ('1', 'Omicron Persei 8', '89475345.3545'), ('2', 'Decapod X', '65498463216.3466'), ('3', 'Mars', '32435021.65468'), ('4', 'Omega III', '98432121.5464'), ('5', 'Tarantulon VI', '849842198.354654'), ('6', 'Cannibalon', '654321987.21654'), ('7', 'DogDoo VII', '65498721354.688'), ('8', 'Nintenduu 64', '6543219894.1654'), ('9', 'Amazonia', '65432135979.6547'); 36 | COMMIT; 37 | 38 | SET FOREIGN_KEY_CHECKS = 1; 39 | -------------------------------------------------------------------------------- /7. Planet Express/Has_Clearance.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:41:31 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Has_Clearance` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Has_Clearance`; 24 | CREATE TABLE `Has_Clearance` ( 25 | `Employee` int(11) NOT NULL, 26 | `Planet` int(11) NOT NULL, 27 | `Level` int(11) NOT NULL, 28 | PRIMARY KEY (`Employee`,`Planet`), 29 | KEY `Planet` (`Planet`), 30 | CONSTRAINT `has_clearance_ibfk_1` FOREIGN KEY (`Employee`) REFERENCES `Employee` (`EmployeeID`), 31 | CONSTRAINT `has_clearance_ibfk_2` FOREIGN KEY (`Planet`) REFERENCES `Planet` (`PlanetID`) 32 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 33 | 34 | -- ---------------------------- 35 | -- Records of `Has_Clearance` 36 | -- ---------------------------- 37 | BEGIN; 38 | INSERT INTO `Has_Clearance` VALUES ('1', '1', '2'), ('1', '2', '3'), ('2', '3', '2'), ('2', '4', '4'), ('3', '5', '2'), ('3', '6', '4'), ('4', '7', '1'); 39 | COMMIT; 40 | 41 | SET FOREIGN_KEY_CHECKS = 1; 42 | -------------------------------------------------------------------------------- /5. Pieces and providers/Provides.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:36:39 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Provides` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Provides`; 24 | CREATE TABLE `Provides` ( 25 | `Piece` int(11) NOT NULL, 26 | `Provider` varchar(40) NOT NULL, 27 | `Price` int(11) NOT NULL, 28 | PRIMARY KEY (`Piece`,`Provider`), 29 | KEY `Provider` (`Provider`), 30 | CONSTRAINT `provides_ibfk_1` FOREIGN KEY (`Piece`) REFERENCES `Pieces` (`Code`), 31 | CONSTRAINT `provides_ibfk_2` FOREIGN KEY (`Provider`) REFERENCES `Providers` (`Code`) 32 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 33 | 34 | -- ---------------------------- 35 | -- Records of `Provides` 36 | -- ---------------------------- 37 | BEGIN; 38 | INSERT INTO `Provides` VALUES ('1', 'HAL', '10'), ('1', 'RBT', '15'), ('2', 'HAL', '20'), ('2', 'RBT', '15'), ('2', 'TNBC', '14'), ('3', 'RBT', '50'), ('3', 'TNBC', '45'), ('4', 'HAL', '5'), ('4', 'RBT', '7'); 39 | COMMIT; 40 | 41 | SET FOREIGN_KEY_CHECKS = 1; 42 | -------------------------------------------------------------------------------- /7. Planet Express/Shipment.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:41:04 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Shipment` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Shipment`; 24 | CREATE TABLE `Shipment` ( 25 | `ShipmentID` int(11) NOT NULL, 26 | `Date` date DEFAULT NULL, 27 | `Manager` int(11) NOT NULL, 28 | `Planet` int(11) NOT NULL, 29 | PRIMARY KEY (`ShipmentID`), 30 | KEY `Manager` (`Manager`), 31 | KEY `Planet` (`Planet`), 32 | CONSTRAINT `shipment_ibfk_1` FOREIGN KEY (`Manager`) REFERENCES `Employee` (`EmployeeID`), 33 | CONSTRAINT `shipment_ibfk_2` FOREIGN KEY (`Planet`) REFERENCES `Planet` (`PlanetID`) 34 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 35 | 36 | -- ---------------------------- 37 | -- Records of `Shipment` 38 | -- ---------------------------- 39 | BEGIN; 40 | INSERT INTO `Shipment` VALUES ('1', '3004-05-11', '1', '1'), ('2', '3004-05-11', '1', '2'), ('3', null, '2', '3'), ('4', null, '2', '4'), ('5', null, '7', '5'); 41 | COMMIT; 42 | 43 | SET FOREIGN_KEY_CHECKS = 1; 44 | -------------------------------------------------------------------------------- /6. Scientists/AssignedTo.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:37:57 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `AssignedTo` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `AssignedTo`; 24 | CREATE TABLE `AssignedTo` ( 25 | `Scientist` int(11) NOT NULL, 26 | `Project` char(4) NOT NULL, 27 | PRIMARY KEY (`Scientist`,`Project`), 28 | KEY `Project` (`Project`), 29 | CONSTRAINT `assignedto_ibfk_1` FOREIGN KEY (`Scientist`) REFERENCES `Scientists` (`SSN`), 30 | CONSTRAINT `assignedto_ibfk_2` FOREIGN KEY (`Project`) REFERENCES `Projects` (`Code`) 31 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 32 | 33 | -- ---------------------------- 34 | -- Records of `AssignedTo` 35 | -- ---------------------------- 36 | BEGIN; 37 | INSERT INTO `AssignedTo` VALUES ('123234877', 'AeH1'), ('152934485', 'AeH3'), ('745685214', 'AeH3'), ('332569843', 'AeH4'), ('845657245', 'Ast1'), ('845657246', 'Ast2'), ('222364883', 'Ast3'), ('326587417', 'Ast3'), ('631231482', 'Ast3'), ('332154719', 'Bte1'), ('546523478', 'Che1'), ('654873219', 'Che1'); 38 | COMMIT; 39 | 40 | SET FOREIGN_KEY_CHECKS = 1; 41 | -------------------------------------------------------------------------------- /3. The warehouse/Boxes.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:31:46 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Boxes` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Boxes`; 24 | CREATE TABLE `Boxes` ( 25 | `Code` varchar(255) NOT NULL, 26 | `Contents` varchar(255) NOT NULL, 27 | `Value` double NOT NULL, 28 | `Warehouse` int(11) NOT NULL, 29 | PRIMARY KEY (`Code`), 30 | KEY `Warehouse` (`Warehouse`), 31 | CONSTRAINT `boxes_ibfk_1` FOREIGN KEY (`Warehouse`) REFERENCES `Warehouses` (`Code`) 32 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 33 | 34 | -- ---------------------------- 35 | -- Records of `Boxes` 36 | -- ---------------------------- 37 | BEGIN; 38 | INSERT INTO `Boxes` VALUES ('0MN7', 'Rocks', '180', '3'), ('4H8P', 'Rocks', '250', '1'), ('4RT3', 'Scissors', '190', '4'), ('7G3H', 'Rocks', '200', '1'), ('8JN6', 'Papers', '75', '1'), ('8Y6U', 'Papers', '50', '3'), ('9J6F', 'Papers', '175', '2'), ('LL08', 'Rocks', '140', '4'), ('P0H6', 'Scissors', '125', '1'), ('P2T6', 'Scissors', '150', '2'), ('TU55', 'Papers', '90', '5'); 39 | COMMIT; 40 | 41 | SET FOREIGN_KEY_CHECKS = 1; 42 | -------------------------------------------------------------------------------- /7. Planet Express/Employee.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:40:48 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Employee` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Employee`; 24 | CREATE TABLE `Employee` ( 25 | `EmployeeID` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | `Position` varchar(255) NOT NULL, 28 | `Salary` double NOT NULL, 29 | `Remarks` varchar(255) DEFAULT NULL, 30 | PRIMARY KEY (`EmployeeID`) 31 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 32 | 33 | -- ---------------------------- 34 | -- Records of `Employee` 35 | -- ---------------------------- 36 | BEGIN; 37 | INSERT INTO `Employee` VALUES ('1', 'Phillip J. Fry', 'Delivery boy', '7500', 'Not to be confused with the Philip J. Fry from Hovering Squid World 97a'), ('2', 'Turanga Leela', 'Captain', '10000', null), ('3', 'Bender Bending Rodriguez', 'Robot', '7500', null), ('4', 'Hubert J. Farnsworth', 'CEO', '20000', null), ('5', 'John A. Zoidberg', 'Physician', '25', null), ('6', 'Amy Wong', 'Intern', '5000', null), ('7', 'Hermes Conrad', 'Bureaucrat', '10000', null), ('8', 'Scruffy Scruffington', 'Janitor', '5000', null); 38 | COMMIT; 39 | 40 | SET FOREIGN_KEY_CHECKS = 1; 41 | -------------------------------------------------------------------------------- /6. Scientists/Projects.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:38:19 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Projects` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Projects`; 24 | CREATE TABLE `Projects` ( 25 | `Code` char(4) NOT NULL, 26 | `Name` char(50) NOT NULL, 27 | `Hours` int(11) DEFAULT NULL, 28 | PRIMARY KEY (`Code`) 29 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 30 | 31 | -- ---------------------------- 32 | -- Records of `Projects` 33 | -- ---------------------------- 34 | BEGIN; 35 | INSERT INTO `Projects` VALUES ('AeH1', 'Winds: Studying Bernoullis Principle', '156'), ('AeH2', 'Aerodynamics and Bridge Design', '189'), ('AeH3', 'Aerodynamics and Gas Mileage', '256'), ('AeH4', 'Aerodynamics and Ice Hockey', '789'), ('AeH5', 'Aerodynamics of a Football', '98'), ('AeH6', 'Aerodynamics of Air Hockey', '89'), ('Ast1', 'A Matter of Time', '112'), ('Ast2', 'A Puzzling Parallax', '299'), ('Ast3', 'Build Your Own Telescope', '6546'), ('Bte1', 'Juicy: Extracting Apple Juice with Pectinase', '321'), ('Bte2', 'A Magnetic Primer Designer', '9684'), ('Bte3', 'Bacterial Transformation Efficiency', '321'), ('Che1', 'A Silver-Cleaning Battery', '545'), ('Che2', 'A Soluble Separation Solution', '778'); 36 | COMMIT; 37 | 38 | SET FOREIGN_KEY_CHECKS = 1; 39 | -------------------------------------------------------------------------------- /2. Employee management/Employees.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 05/22/2019 15:28:03 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Employees` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Employees`; 24 | CREATE TABLE `Employees` ( 25 | `SSN` int(11) NOT NULL, 26 | `Name` varchar(255) NOT NULL, 27 | `LastName` varchar(255) NOT NULL, 28 | `Department` int(11) NOT NULL, 29 | PRIMARY KEY (`SSN`), 30 | KEY `Department` (`Department`), 31 | CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`Department`) REFERENCES `Departments` (`Code`) 32 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 33 | 34 | -- ---------------------------- 35 | -- Records of `Employees` 36 | -- ---------------------------- 37 | BEGIN; 38 | INSERT INTO `Employees` VALUES ('123234877', 'Michael', 'Rogers', '14'), ('152934485', 'Anand', 'Manikutty', '14'), ('222364883', 'Carol', 'Smith', '37'), ('326587417', 'Joe', 'Stevens', '37'), ('332154719', 'Mary-Anne', 'Foster', '14'), ('332569843', 'George', 'O\'Donnell', '77'), ('546523478', 'John', 'Doe', '59'), ('631231482', 'David', 'Smith', '77'), ('654873219', 'Zacary', 'Efron', '59'), ('745685214', 'Eric', 'Goldsmith', '59'), ('845657245', 'Elizabeth', 'Doe', '14'), ('845657246', 'Kumar', 'Swamy', '14'); 39 | COMMIT; 40 | 41 | SET FOREIGN_KEY_CHECKS = 1; 42 | -------------------------------------------------------------------------------- /1. The computer store/build_schema.sql: -------------------------------------------------------------------------------- 1 | # Table creation 2 | CREATE TABLE Manufacturers ( 3 | Code INTEGER, 4 | Name VARCHAR(255) NOT NULL, 5 | PRIMARY KEY (Code) 6 | ); 7 | 8 | CREATE TABLE Products ( 9 | Code INTEGER, 10 | Name VARCHAR(255) NOT NULL, 11 | Price DECIMAL NOT NULL, 12 | Manufacturer INTEGER NOT NULL, 13 | PRIMARY KEY (Code), 14 | FOREIGN KEY (Manufacturer) REFERENCES Manufacturers(Code) 15 | ) ENGINE=INNODB; 16 | 17 | 18 | # Sample dataset 19 | INSERT INTO Manufacturers(Code, Name) VALUES(1,'Sony'); 20 | INSERT INTO Manufacturers(Code, Name) VALUES(2,'Creative Labs'); 21 | INSERT INTO Manufacturers(Code, Name) VALUES(3,'Hewlett-Packard'); 22 | INSERT INTO Manufacturers(Code, Name) VALUES(4,'Iomega'); 23 | INSERT INTO Manufacturers(Code, Name) VALUES(5,'Fujitsu'); 24 | INSERT INTO Manufacturers(Code, Name) VALUES(6,'Winchester'); 25 | 26 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(1, 'Hard drive', 240, 5); 27 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(2, 'Memory', 120, 6); 28 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(3, 'ZIP drive', 150, 4); 29 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(4, 'Floppy disk', 5, 6); 30 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(5, 'Monitor', 240, 1); 31 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(6, 'DVD drive', 180, 2); 32 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(7, 'CD drive', 90, 2); 33 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(8, 'Printer', 270, 3); 34 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(9, 'Toner cartridge', 66, 3); 35 | INSERT INTO Products(Code, Name, Price, Manufacturer) VALUES(10, 'DVD burner', 180, 2); 36 | -------------------------------------------------------------------------------- /7. Planet Express/Package.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : localhost 5 | Source Server Type : MySQL 6 | Source Server Version : 50716 7 | Source Host : localhost 8 | Source Database : sql-problems 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50716 12 | File Encoding : utf-8 13 | 14 | Date: 07/01/2019 21:41:45 PM 15 | */ 16 | 17 | SET NAMES utf8mb4; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `Package` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `Package`; 24 | CREATE TABLE `Package` ( 25 | `Shipment` int(11) NOT NULL, 26 | `PackageNumber` int(11) NOT NULL, 27 | `Contents` varchar(255) NOT NULL, 28 | `Weight` double NOT NULL, 29 | `Sender` int(11) NOT NULL, 30 | `Recipient` int(11) NOT NULL, 31 | PRIMARY KEY (`Shipment`,`PackageNumber`), 32 | KEY `Sender` (`Sender`), 33 | KEY `Recipient` (`Recipient`), 34 | CONSTRAINT `package_ibfk_1` FOREIGN KEY (`Shipment`) REFERENCES `Shipment` (`ShipmentID`), 35 | CONSTRAINT `package_ibfk_2` FOREIGN KEY (`Sender`) REFERENCES `Client` (`AccountNumber`), 36 | CONSTRAINT `package_ibfk_3` FOREIGN KEY (`Recipient`) REFERENCES `Client` (`AccountNumber`) 37 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 38 | 39 | -- ---------------------------- 40 | -- Records of `Package` 41 | -- ---------------------------- 42 | BEGIN; 43 | INSERT INTO `Package` VALUES ('1', '1', 'Undeclared', '1.5', '1', '2'), ('2', '1', 'Undeclared', '10', '2', '3'), ('2', '2', 'A bucket of krill', '2', '8', '7'), ('3', '1', 'Undeclared', '15', '3', '4'), ('3', '2', 'Undeclared', '3', '5', '1'), ('3', '3', 'Undeclared', '7', '2', '3'), ('4', '1', 'Undeclared', '5', '4', '5'), ('4', '2', 'Undeclared', '27', '1', '2'), ('5', '1', 'Undeclared', '100', '5', '1'); 44 | COMMIT; 45 | 46 | SET FOREIGN_KEY_CHECKS = 1; 47 | -------------------------------------------------------------------------------- /4. Movie theatres/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 1. Select the title of all movies. 4 | 5 | ```sql 6 | SELECT Title 7 | FROM Movies 8 | ; 9 | ``` 10 | 11 | 2. Show all the distinct ratings in the database. 12 | 13 | ```sql 14 | SELECT DISTINCT Rating 15 | FROM Movies 16 | ; 17 | ``` 18 | 19 | 3. Show all unrated movies. 20 | 21 | ```sql 22 | SELECT * 23 | FROM Movies 24 | WHERE Rating IS NULL 25 | ; 26 | ``` 27 | 28 | 4. Select all movie theaters that are not currently showing a movie. 29 | 30 | ```sql 31 | SELECT * 32 | FROM MovieTheaters 33 | WHERE Movie IS NULL 34 | ; 35 | ``` 36 | 37 | 5. Select all data from all movie theaters and, additionally, the data from the movie that is being shown in the theater (if one is being shown). 38 | 39 | ```sql 40 | SELECT * 41 | FROM MovieTheaters 42 | LEFT JOIN Movies 43 | ON MovieTheaters.Movie = Movies.Code 44 | ; 45 | ``` 46 | 47 | 6. Select all data from all movies and, if that movie is being shown in a theater, show the data from the theater. 48 | 49 | ```sql 50 | SELECT * 51 | FROM MovieTheaters 52 | RIGHT JOIN Movies 53 | ON MovieTheaters.Movie = Movies.Code 54 | ; 55 | ``` 56 | 57 | 7. Show the titles of movies not currently being shown in any theaters. 58 | 59 | ```sql 60 | SELECT 61 | FROM Movies AS M 62 | WHERE M.Code NOT IN (SELECT T.Movie 63 | FROM MovieTheaters AS T 64 | WHERE T.Movie IS NOT NULL) 65 | ; 66 | ``` 67 | 68 | 8. Add the unrated movie "One, Two, Three". 69 | 70 | ```sql 71 | INSERT INTO Movies(Title, Rating) 72 | VALUES ('One, Two, Three', NULL) 73 | ; 74 | ``` 75 | 76 | 9. Set the rating of all unrated movies to "G". 77 | 78 | ```sql 79 | UPDATE Movies 80 | SET Rating = 'G' 81 | WHERE Rating IS NULL 82 | ; 83 | ``` 84 | 85 | 10. Remove movie theaters projecting movies rated "NC-17". 86 | 87 | ```sql 88 | DELETE FROM MovieTheaters 89 | WHERE Movie IN (SELECT Code 90 | FROM Movies 91 | WHERE Rating = 'NC-17' 92 | ) 93 | ; 94 | ``` 95 | -------------------------------------------------------------------------------- /5. Pieces and providers/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 4 | 1. Select the name of all the pieces. 5 | 6 | ```sql 7 | SELECT Name 8 | FROM Pieces 9 | ; 10 | ``` 11 | 12 | 2. Select all the providers' data. 13 | 14 | ```sql 15 | SELECT * 16 | FROM Providers 17 | ; 18 | ``` 19 | 20 | 3. Obtain the average price of each piece (show only the piece code and the average price). 21 | 22 | ```sql 23 | SELECT Pi.Code, AVG(Pr.Price) 24 | FROM Pieces AS Pi 25 | INNER JOIN Providers AS Pr 26 | ON Pi.Code = Pr.Piece 27 | ; 28 | ``` 29 | 30 | 4. Obtain the names of all providers who supply piece 1. 31 | 32 | ```sql 33 | SELECT * 34 | FROM Providers AS Prr 35 | INNER JOIN Provides AS Pr 36 | ON Prr.Code = Pr.Provider 37 | AND Pr.Piece = 1 38 | ; 39 | ``` 40 | 41 | 5. Select the name of pieces provided by provider with code "HAL". 42 | 43 | ```sql 44 | SELECT Pieces.Name 45 | FROM Pieces 46 | INNER JOIN Provides 47 | ON Pieces.Code = Provides.Piece 48 | AND Provides.Provider = 'HAL' 49 | ; 50 | ``` 51 | 52 | ***Optional solutions:*** 53 | 54 | ```sql 55 | /* With IN subquery */ 56 | SELECT Name 57 | FROM Pieces 58 | WHERE Code IN (SELECT Piece 59 | FROM Provides 60 | WHERE Provider = 'HAL' 61 | ) 62 | ; 63 | 64 | 65 | /* With EXISTS subquery */ 66 | SELECT Name 67 | FROM Pieces 68 | WHERE EXISTS (SELECT * 69 | FROM Provides 70 | WHERE Provider = 'HAL' 71 | AND Piece = Pieces.Code 72 | ) 73 | ; 74 | ``` 75 | 76 | 6. For each piece, find the most expensive offering of that piece and include the piece name, provider name, and price **(note that there could be two providers who *supply the same piece at the most expensive price*)**. 77 | 78 | ```sql 79 | SELECT Pieces.Name, Providers.Name, Price 80 | FROM Pieces INNER JOIN Provides ON Pieces.Code = Piece 81 | INNER JOIN Providers ON Providers.Code = Provider 82 | WHERE Price = ( 83 | SELECT MAX(Pr.Price) 84 | FROM Provides AS Pr 85 | WHERE Piece = Pieces.Code 86 | ) 87 | ; 88 | ``` 89 | 90 | 7. Add an entry to the database to indicate that "Skellington Supplies" (code "TNBC") will provide sprockets (code "1") for 7 cents each. 91 | 92 | ```sql 93 | INSERT INTO Provides 94 | VALUES (1, 'TNBC', 7) 95 | ; 96 | ``` 97 | 98 | 8. Increase all prices by one cent. _(I suppose price in the table is measured using *dollar*)_ 99 | 100 | ```sql 101 | UPDATE Provides SET Price = Price + 0.01; 102 | ``` 103 | 104 | 9. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply bolts (code 4). 105 | 106 | ```sql 107 | DELETE FROM Provides 108 | WHERE Provider = 'RBT' 109 | AND Piece = 4 110 | ; 111 | ``` 112 | 113 | 10. Update the database to reflect that "Susan Calvin Corp." (code "RBT") will not supply any pieces (the provider should still remain in the database). 114 | 115 | ```sql 116 | DELETE FROM Provides 117 | WHERE Provider = 'RBT' 118 | ; 119 | ``` 120 | -------------------------------------------------------------------------------- /3. The warehouse/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 4 | 1. Select all warehouses. 5 | 6 | ```sql 7 | SELECT * 8 | FROM Warehouses 9 | ; 10 | ``` 11 | 12 | 2. Select all boxes with a value larger than $150. 13 | 14 | ```sql 15 | SELECT * 16 | FROM Boxes 17 | WHERE Value > 150 18 | ; 19 | ``` 20 | 21 | 3. Select all distinct contents in all the boxes. 22 | 23 | ```sql 24 | SELECT DISTINCT(Contents) 25 | FROM Boxes 26 | ; 27 | ``` 28 | 29 | 4. Select the average value of all the boxes. 30 | 31 | ```sql 32 | SELECT AVG(Value) 33 | FROM Boxes 34 | ; 35 | ``` 36 | 37 | 5. Select the warehouse code and the average value of the boxes in each warehouse. 38 | 39 | ```sql 40 | SELECT Warehouse, AVG(Value) 41 | FROM Boxes 42 | GROUP BY Warehouse 43 | ; 44 | ``` 45 | 46 | 6. Same as previous exercise, but select only those warehouses where the average value of the boxes is greater than 150. 47 | 48 | ```sql 49 | SELECT Warehouse, AVG(Value) 50 | FROM Boxes 51 | GROUP BY Warehouse 52 | HAVING AVG(Value) > 150 53 | ; 54 | ``` 55 | 56 | 7. Select the code of each box, along with the name of the city the box is located in. 57 | 58 | ```sql 59 | SELECT W.Code, W.Location 60 | FROM Warehouses AS W 61 | INNER JOIN Boxes AS B 62 | ON W.Code = B.Warehouse 63 | ; 64 | ``` 65 | 66 | 8. Select the warehouse codes, along with the number of boxes in each warehouse. Optionally, take into account that some warehouses are empty (i.e., the box count should show up as zero, instead of omitting the warehouse from the result). 67 | 68 | ```sql 69 | SELECT W.Code, COUNT(B.Code) 70 | FROM Warehouses AS W 71 | LEFT JOIN Boxes AS B 72 | ON W.Code = B.Warehouse 73 | GROUP BY B.Code 74 | ; 75 | ``` 76 | 77 | 9. Select the codes of all warehouses that are saturated (a warehouse is saturated if the number of boxes in it is larger than the warehouse's capacity). 78 | 79 | ```sql 80 | SELECT W.Code 81 | FROM Warehouses AS W 82 | INNER JOIN Boxes AS B 83 | ON W.Code = B.Warehouse 84 | GROUP BY W.Code 85 | WHERE COUNT(B.Code) > W.Capacity 86 | ; 87 | ``` 88 | 89 | 10. Select the codes of all the boxes located in Chicago. 90 | 91 | ```sql 92 | SELECT B.Code 93 | FROM Boxes AS B 94 | INNER JOIN Warehouses AS W 95 | ON W.Code = B.Warehouse 96 | WHERE W.Location = 'Chicago' 97 | ; 98 | ``` 99 | 100 | 11. Create a new warehouse in New York with a capacity for 3 boxes. 101 | 102 | ```sql 103 | INSERT INTO Warehouses(Location, Capacity) 104 | VALUES ('New York', 3) 105 | ; 106 | ``` 107 | 108 | 12. Create a new box, with code "H5RT", containing "Papers" with a value of $200, and located in warehouse 2. 109 | 110 | ```sql 111 | INSERT INTO Boxes(Code, Contents, Value, Warehouse) 112 | VALUES ('H5RT', 'Papers', 200, 2) 113 | ; 114 | ``` 115 | 116 | 13. Reduce the value of all boxes by 15%. 117 | 118 | ```sql 119 | UPDATE Boxes 120 | SET Value = Value * 0.85 121 | ; 122 | ``` 123 | 124 | 14. Apply a 20% value reduction to boxes with a value larger than the average value of all the boxes. 125 | 126 | > Attention: this solution doesn't work with MySQL 5.0.67: ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause 127 | 128 | ```sql 129 | UPDATE Boxes 130 | SET Value = Value * 0.8 131 | WHERE Value > (SELECT AVG(Value) 132 | FROM Boxes 133 | ) 134 | ; 135 | ``` 136 | 137 | 15. Remove all boxes with a value lower than $100. 138 | 139 | ```sql 140 | DELETE FROM Boxes 141 | WHERE Value < 100 142 | ; 143 | ``` 144 | 145 | 16. Remove all boxes from saturated warehouses. 146 | 147 | > Attention: this solution doesn't work with MySQL 5.0.67: ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause 148 | 149 | ```sql 150 | DELTE FROM Boxes 151 | WHERE Warehouse IN ( 152 | SELECT Code 153 | FROM Warehouses AS W 154 | WHERE Capacity < (SELECT COUNT(B.Code) 155 | FROM Boxes AS B 156 | WHERE B.Warehouse = W.Code 157 | ) 158 | ) 159 | ; 160 | ``` 161 | -------------------------------------------------------------------------------- /2. Employee management/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 1. Select the last name of all employees. 4 | 5 | ```sql 6 | SELECT LastName 7 | FROM Employees 8 | ; 9 | ``` 10 | 11 | 2. Select the last name of all employees, without duplicates. 12 | 13 | ```sql 14 | SELECT DISTINCT LastName 15 | FROM Employees 16 | ; 17 | ``` 18 | 19 | 3. Select all the data of employees whose last name is "Smith". 20 | 21 | ```sql 22 | SELECT * 23 | FROM Employees 24 | WHERE LastName = 'Smith' 25 | ; 26 | ``` 27 | 28 | 4. Select all the data of employees whose last name is "Smith" or "Doe". 29 | 30 | ```sql 31 | SELECT * 32 | FROM Employees 33 | WHERE LastName = 'Smith' 34 | OR LastName = 'Doe' 35 | ; 36 | ``` 37 | 38 | ```sql 39 | /* With IN */ 40 | SELECT * 41 | FROM Employees 42 | WHERE LastName IN ('Smith', 'Doe') 43 | ; 44 | ``` 45 | 46 | 5. Select all the data of employees that work in department 14. 47 | 48 | ```sql 49 | SELECT * 50 | FROM Employees 51 | WHERE Department = 14 52 | ; 53 | ``` 54 | 55 | 6. Select all the data of employees that work in department 37 or department 77. 56 | 57 | ```sql 58 | SELECT * 59 | FROM Employees 60 | WHERE Department = 14 61 | OR Department = 77 62 | ; 63 | ``` 64 | 65 | 7. Select all the data of employees whose last name begins with an "S". 66 | 67 | ```sql 68 | SELECT * 69 | FROM Employees 70 | WHERE LastName LIKE 'S%' 71 | ; 72 | ``` 73 | 74 | 8. Select the sum of all the departments' budgets. 75 | 76 | ```sql 77 | SELECT SUM(Budget) 78 | FROM Departments 79 | ; 80 | ``` 81 | 82 | 9. Select the number of employees in each department (you only need to show the department code and the number of employees). 83 | 84 | ```sql 85 | SELECT D.Code, COUNT(*) 86 | FROM Employees AS E, Departments AS D 87 | GROUP BY D.Code 88 | ; 89 | ``` 90 | 91 | 10. Select all the data of employees, including each employee's department's data. 92 | 93 | ```sql 94 | SELECT * 95 | FROM Employees AS E 96 | INNER JOIN Departments AS D 97 | ON D.Code = E.Department 98 | ; 99 | ``` 100 | 101 | 11. Select the name and last name of each employee, along with the name and budget of the employee's department. 102 | 103 | ```sql 104 | SELECT E.Name, E.LastName, D.Name, D.Budget 105 | FROM Employees AS E 106 | INNER JOIN Departments AS D 107 | ON E.Department = D.Code 108 | ; 109 | ``` 110 | 111 | 12. Select the name and last name of employees working for departments with a budget greater than $60,000. 112 | 113 | ```sql 114 | SELECT E.Name, E.LastName 115 | FROM Employees AS E 116 | INNER JOIN Departments AS D 117 | ON E.Department = D.Code 118 | AND D.Budget > 60000 119 | ; 120 | ``` 121 | 122 | 13. Select the departments with a budget larger than the average budget of all the departments. 123 | 124 | ```sql 125 | SELECT * 126 | FROM Departments 127 | WHERE Budget > (SELECT AVG(D.Budget) 128 | FROM Departments AS D 129 | ) 130 | ; 131 | ``` 132 | 133 | 14. Select the names of departments with more than two employees. 134 | 135 | ```sql 136 | SELECT D.Name 137 | FROM Departments AS D 138 | INNER JOIN Employees AS E 139 | ON E.Department = D.Code 140 | GROUP BY D.Code 141 | HAVING COUNT(E.SSN) > 2 142 | ; 143 | ``` 144 | 145 | 15. Select the name and last name of employees working for departments with second lowest budget. 146 | 147 | ```sql 148 | SELECT E.Name, E.LastName 149 | FROM Departments AS D 150 | INNER JOIN Employees AS E 151 | ON E.Department = D.Code 152 | ORDER BY D.Budget 153 | LIMIT 1 OFFSET 1 154 | ; 155 | ``` 156 | 157 | 16. Add a new department called "Quality Assurance", with a budget of $40,000 and departmental code 11. Add an employee called "Mary Moore" in that department, with SSN 847-21-9811. 158 | 159 | ```sql 160 | INSERT INTO Departments(Name, Budge, Code) 161 | VALUES ('Quality Assurance', 40000, 11) 162 | ; 163 | 164 | INSERT INTO Employees(SSN, Name, LastName, Department) 165 | VALUES ('847219811', 'Mary Moore', 'Moore', 11) 166 | ; 167 | ``` 168 | 169 | > Whether adding quotes to integers in SQL Query, please refer to this [question](https://stackoverflow.com/questions/6781976/mysql-quote-numbers-or-not) in Stack Overflow. 170 | 171 | 17. Reduce the budget of all departments by 10%. 172 | 173 | ```sql 174 | UPDATE Departments 175 | SET Budget = Budget * 0.9 176 | ; 177 | ``` 178 | 179 | 18. Reassign all employees from the Research department (code 77) to the IT department (code 14). 180 | 181 | ```sql 182 | UPDATE Employees 183 | SET Department = 14 184 | WHERE Department = 77 185 | ; 186 | ``` 187 | 188 | 19. Delete from the table all employees in the IT department (code 14). 189 | 190 | ```sql 191 | DELETE FROM Employees 192 | WHERE Department = 14 193 | ; 194 | ``` 195 | 196 | 20. Delete from the table all employees who work in departments with a budget greater than or equal to $60,000. 197 | 198 | ```sql 199 | DELETE FROM Employees 200 | WHERE Department IN 201 | (SELECT Code FROM Departments 202 | WHERE Budget >= 60000 203 | ) 204 | ; 205 | ``` 206 | 207 | 21. Delete from the table all employees. 208 | 209 | ```sql 210 | DELETE FROM Employees; 211 | ``` 212 | -------------------------------------------------------------------------------- /1. The computer store/README.md: -------------------------------------------------------------------------------- 1 | ![](https://upload.wikimedia.org/wikipedia/commons/b/b2/Computer-store-db.png) 2 | 3 | > Please note the datatypes given are `SQLite` datatypes. 4 | > 5 | > `PK` and `FK` stand for **primary key** and **foreign key** respectively. 6 | 7 | 8 | 1. Select the names of all the products in the store. 9 | 10 | ```sql 11 | SELECT Name 12 | FROM Products 13 | ; 14 | ``` 15 | 16 | 2. Select the names and the prices of all the products in the store. 17 | 18 | ```sql 19 | SELECT Name, Price 20 | FROM Products 21 | ; 22 | ``` 23 | 24 | 3. Select the name of the products with a price less than or equal to `$200`. 25 | 26 | ```sql 27 | SELECT Name 28 | FROM Products 29 | WHERE Price <= 200 30 | ; 31 | ``` 32 | 33 | 4. Select all the products with a price between `$60` and `$120`. 34 | 35 | ```sql 36 | SELECT * 37 | FROM Products 38 | WHERE Price >= 60 39 | AND Price <= 120 40 | ; 41 | ``` 42 | 43 | 5. Select the name and price in cents (i.e., the price must be multiplied by `100`). 44 | 45 | ```sql 46 | SELECT Name, Price * 100 47 | FROM Products 48 | ; 49 | ``` 50 | 51 | 6. Compute the average price of all the products. 52 | 53 | ```sql 54 | SELECT AVG(Price) 55 | FROM Products 56 | ; 57 | ``` 58 | 59 | 7. Compute the average price of all products with manufacturer code equal to `2`. 60 | 61 | ```sql 62 | SELECT AVG(p.Price) 63 | FROM Products AS p, Manufactures AS m 64 | WHERE m.code = 2 65 | ; 66 | ``` 67 | 68 | 8. Compute the number of products with a price larger than or equal to `$180`. 69 | 70 | ```sql 71 | SELECT COUNT(*) 72 | FROM Products AS p 73 | WHERE p.Price >= 180 74 | ; 75 | ``` 76 | 77 | 9. 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). 78 | 79 | ```sql 80 | SELECT p.Name AS Name, p.Price AS Price 81 | FROM Products AS p 82 | WHERE p.Price >= 180 83 | ORDER BY Price DESC, Name 84 | ; 85 | ``` 86 | 87 | 10. Select all the data from the products, including all the data for each product's manufacturer. 88 | 89 | ```sql 90 | /* Without INNER JOIN */ 91 | SELECT * 92 | FROM Products AS p, Manufacturers AS m 93 | WHERE p.Manufacture == m.Code 94 | ; 95 | 96 | /* With INNER JOIN */ 97 | SELECT * 98 | FROM Products INNER JOIN Manufacturers 99 | ON Products.Manufacturer = Manufacturers.Code 100 | ; 101 | ``` 102 | 103 | 11. Select the product name, price, and manufacturer name of all the products. 104 | 105 | ```sql 106 | /* Without INNER JOIN */ 107 | SELECT p.Name, p.Price, m.Name 108 | FROM Products AS p, Manufacturers AS m 109 | WHERE p.Manufacturer == m.Code 110 | ; 111 | 112 | /* With INNER JOIN */ 113 | SELECT Products.Name, Price, Manufacturers.Name 114 | FROM Products INNER JOIN Manufacturers 115 | ON Products.Manufacturer = Manufacturers.Code 116 | ; 117 | ``` 118 | 119 | 12. Select the average price of each manufacturer's products, showing only the manufacturer's code. 120 | 121 | ```sql 122 | SELECT AVG(p.Price), m.Code 123 | FROM Products AS p, Manufacturers AS m 124 | WHERE p.Manufacturer == m.Code 125 | GROUP BY p.Manufacture 126 | ; 127 | ``` 128 | 129 | 13. Select the average price of each manufacturer's products, showing the manufacturer's name. 130 | 131 | ```sql 132 | /* Without INNER JOIN */ 133 | SELECT AVG(p.Price) AS Price, m.Name AS Name 134 | FROM Products AS p, Manufacturers AS m 135 | WHERE p.Manufacturer = m.Code 136 | GROUP BY m.Name 137 | ; 138 | 139 | /* With INNER JOIN */ 140 | SELECT AVG(Price), Manufacturers.Name 141 | FROM Products INNER JOIN Manufacturers 142 | ON Products.Manufacturer = Manufacturers.Code 143 | GROUP BY Manufacturers.Name 144 | ; 145 | ``` 146 | 147 | 14. Select the names of manufacturer whose products have an average price larger than or equal to `$150`. 148 | 149 | ```sql 150 | /* Without INNER JOIN */ 151 | SELECT m.Name 152 | FROM Products AS p, Manufacturers AS m 153 | WHERE m.Code = p.Manufacturer 154 | GROUP BY m.Name 155 | HAVING AVG(p.Price) >= 150 156 | ; 157 | 158 | /* With INNER JOIN */ 159 | SELECT Manufacturers.Name 160 | FROM Products INNER JOIN Manufacturers 161 | ON Products.Manufacturer = Manufacturers.Code 162 | GROUP BY Manufacturers.Name 163 | HAVING AVG(Price) >= 150 164 | ; 165 | ``` 166 | 167 | 15. Select the name and price of the cheapest product. 168 | 169 | ```sql 170 | SELECT p.Name, p.Price 171 | FROM Products AS p 172 | ORDER BY p.Price 173 | LIMIT 1 174 | ; 175 | 176 | /* With a nested SELECT */ 177 | SELECT Name, Price 178 | FROM Products 179 | WHERE Price = (SELECT MIN(Price) FROM Products) 180 | ; 181 | ``` 182 | 183 | 16. Select the name of each manufacturer along with the name and price of its most expensive product. 184 | 185 | ```sql 186 | /* With a nested SELECT and without INNER JOIN */ 187 | SELECT A.Name, A.Price, F.Name 188 | FROM Products A, Manufacturers F 189 | WHERE A.Manufacturer = F.Code 190 | AND A.Price = 191 | ( 192 | SELECT MAX(A.Price) 193 | FROM Products A 194 | WHERE A.Manufacturer = F.Code 195 | ); 196 | 197 | /* With a nested SELECT and an INNER JOIN */ 198 | SELECT A.Name, A.Price, F.Name 199 | FROM Products A INNER JOIN Manufacturers F 200 | ON A.Manufacturer = F.Code 201 | AND A.Price = 202 | ( 203 | SELECT MAX(A.Price) 204 | FROM Products A 205 | WHERE A.Manufacturer = F.Code 206 | ); 207 | ``` 208 | 209 | 17. Add a new product: Loudspeakers, $70, manufacturer 2. 210 | 211 | ```sql 212 | INSERT INTO Products(Name , Price , Manufacturer) 213 | VALUES ('Loudspeakers', 70, 2) 214 | ; 215 | ``` 216 | 217 | 18. Update the name of product 8 to "Laser Printer". 218 | 219 | ```sql 220 | UPDATE Products 221 | SET Name = 'Laser Printer' 222 | WHERE Code = 8 223 | ; 224 | ``` 225 | 226 | 19. Apply a 10% discount to all products. 227 | 228 | ```sql 229 | UPDATE Products 230 | SET Price = Price * 0.9 231 | ; 232 | ``` 233 | 234 | 20. Apply a 10% discount to all products with a price larger than or equal to `$120`. 235 | 236 | ```sql 237 | UPDATE Products 238 | SET Price = Price * 0.9 239 | WHERE Price >= 120 240 | ; 241 | ``` 242 | -------------------------------------------------------------------------------- /8. The Hospital/README.md: -------------------------------------------------------------------------------- 1 | Employees 2 | 3 | 4 | 1. Obtain the names of all physicians that have performed a medical procedure they have never been certified to perform. 5 | 6 | ```sql 7 | SELECT Name 8 | FROM Physician 9 | WHERE EmployeeID IN 10 | ( 11 | SELECT Physician 12 | FROM Undergoes U 13 | WHERE NOT EXISTS 14 | ( 15 | SELECT * 16 | FROM Trained_In 17 | WHERE Treatment = Procedure 18 | AND Physician = U.Physician 19 | ) 20 | ) 21 | ; 22 | ``` 23 | 24 | 2. Same as the previous query, but include the following information in the results: Physician name, name of procedure, date when the procedure was carried out, name of the patient the procedure was carried out on. 25 | 26 | ```sql 27 | SELECT P.Name AS Physician, Pr.Name AS Procedure, U.Date, Pt.Name AS Patient 28 | FROM Physician P, Undergoes U, Patient Pt, Procedure Pr 29 | WHERE U.Patient = Pt.SSN 30 | AND U.Procedure = Pr.Code 31 | AND U.Physician = P.EmployeeID 32 | AND NOT EXISTS 33 | ( 34 | SELECT * FROM Trained_In T 35 | WHERE T.Treatment = U.Procedure 36 | AND T.Physician = U.Physician 37 | ); 38 | ``` 39 | 40 | 3. Obtain the names of all physicians that have performed a medical procedure that they are certified to perform, but such that the procedure was done at a date (Undergoes.Date) after the physician's certification expired (Trained_In.CertificationExpires). 41 | 42 | ```sql 43 | SELECT Name 44 | FROM Physician 45 | WHERE EmployeeID IN 46 | ( 47 | SELECT Physician FROM Undergoes U 48 | WHERE Date > 49 | ( 50 | SELECT CertificationExpires 51 | FROM Trained_In T 52 | WHERE T.Physician = U.Physician 53 | AND T.Treatment = U.Procedure 54 | ) 55 | ); 56 | ``` 57 | 58 | 4. Same as the previous query, but include the following information in the results: Physician name, name of procedure, date when the procedure was carried out, name of the patient the procedure was carried out on, and date when the certification expired. 59 | 60 | ```sql 61 | SELECT P.Name AS Physician, Pr.Name AS Procedure, U.Date, Pt.Name AS Patient, T.CertificationExpires 62 | FROM Physician P, Undergoes U, Patient Pt, Procedure Pr, Trained_In T 63 | WHERE U.Patient = Pt.SSN 64 | AND U.Procedure = Pr.Code 65 | AND U.Physician = P.EmployeeID 66 | AND Pr.Code = T.Treatment 67 | AND P.EmployeeID = T.Physician 68 | AND U.Date > T.CertificationExpires; 69 | ``` 70 | 71 | 5. Obtain the information for appointments where a patient met with a physician other than his/her primary care physician. Show the following information: Patient name, physician name, nurse name (if any), start and end time of appointment, examination room, and the name of the patient's primary care physician. 72 | 73 | ```sql 74 | SELECT Pt.Name AS Patient, Ph.Name AS Physician, N.Name AS Nurse, A.Start, A.End, A.ExaminationRoom, PhPCP.Name AS PCP 75 | FROM Patient Pt, Physician Ph, Physician PhPCP, Appointment A LEFT JOIN Nurse N ON A.PrepNurse = N.EmployeeID 76 | WHERE A.Patient = Pt.SSN 77 | AND A.Physician = Ph.EmployeeID 78 | AND Pt.PCP = PhPCP.EmployeeID 79 | AND A.Physician <> Pt.PCP; 80 | ``` 81 | 82 | 6. The Patient field in Undergoes is redundant, since we can obtain it from the Stay table. There are no constraints in force to prevent inconsistencies between these two tables. More specifically, the Undergoes table may include a row where the patient ID does not match the one we would obtain from the Stay table through the Undergoes.Stay foreign key. Select all rows from Undergoes that exhibit this inconsistency. 83 | 84 | ```sql 85 | SELECT * FROM Undergoes U 86 | WHERE Patient <> 87 | ( 88 | SELECT Patient FROM Stay S 89 | WHERE U.Stay = S.StayID 90 | ); 91 | ``` 92 | 93 | 7. Obtain the names of all the nurses who have ever been on call for room 123. 94 | 95 | ```sql 96 | SELECT N.Name FROM Nurse N 97 | WHERE EmployeeID IN 98 | ( 99 | SELECT OC.Nurse FROM On_Call OC, Room R 100 | WHERE OC.BlockFloor = R.BlockFloor 101 | AND OC.BlockCode = R.BlockCode 102 | AND R.Number = 123 103 | ); 104 | ``` 105 | 106 | 8. The hospital has several examination rooms where appointments take place. Obtain the number of appointments that have taken place in each examination room. 107 | 108 | > N.b. The solution below fails in MS `SQL Server Management Studio`, with the following message: 109 | 110 | ``` 111 | Msg 306, Level 16, State 2, Line 473 112 | The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. 113 | ``` 114 | 115 | ```sql 116 | SELECT ExaminationRoom, COUNT(AppointmentID) AS Number 117 | FROM Appointment 118 | GROUP BY ExaminationRoom 119 | ; 120 | ``` 121 | 122 | 9. Obtain the names of all patients (also include, for each patient, the name of the patient's primary care physician), such that \emph{all} the following are true: 123 | - The patient has been prescribed some medication by his/her primary care physician. 124 | - The patient has undergone a procedure with a cost larger that $5,000 125 | - The patient has had at least two appointment where the nurse who prepped the appointment was a registered nurse. 126 | - The patient's primary care physician is not the head of any department. 127 | 128 | ```sql 129 | SELECT Pt.Name, PhPCP.Name FROM Patient Pt, Physician PhPCP 130 | WHERE Pt.PCP = PhPCP.EmployeeID 131 | AND EXISTS 132 | ( 133 | SELECT * FROM Prescribes Pr 134 | WHERE Pr.Patient = Pt.SSN 135 | AND Pr.Physician = Pt.PCP 136 | ) 137 | AND EXISTS 138 | ( 139 | SELECT * FROM Undergoes U, Procedure Pr 140 | WHERE U.Procedure = Pr.Code 141 | AND U.Patient = Pt.SSN 142 | AND Pr.Cost > 5000 143 | ) 144 | AND 2 <= 145 | ( 146 | SELECT COUNT(A.AppointmentID) FROM Appointment A, Nurse N 147 | WHERE A.PrepNurse = N.EmployeeID 148 | AND N.Registered = 1 149 | ) 150 | AND NOT Pt.PCP IN 151 | ( 152 | SELECT Head FROM Department 153 | ); 154 | ``` -------------------------------------------------------------------------------- /8. The Hospital/build_schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS Physician; 2 | CREATE TABLE Physician ( 3 | EmployeeID INTEGER NOT NULL, 4 | Name VARCHAR(30) NOT NULL, 5 | Position VARCHAR(30) NOT NULL, 6 | SSN INTEGER NOT NULL, 7 | CONSTRAINT pk_physician PRIMARY KEY(EmployeeID) 8 | ); 9 | 10 | DROP TABLE IF EXISTS Department; 11 | CREATE TABLE Department ( 12 | DepartmentID INTEGER NOT NULL, 13 | Name VARCHAR(30) NOT NULL, 14 | Head INTEGER NOT NULL, 15 | CONSTRAINT pk_Department PRIMARY KEY(DepartmentID), 16 | CONSTRAINT fk_Department_Physician_EmployeeID FOREIGN KEY(Head) REFERENCES Physician(EmployeeID) 17 | ); 18 | 19 | 20 | DROP TABLE IF EXISTS Affiliated_With; 21 | CREATE TABLE Affiliated_With ( 22 | Physician INTEGER NOT NULL, 23 | Department INTEGER NOT NULL, 24 | PrimaryAffiliation BOOLEAN NOT NULL, 25 | CONSTRAINT fk_Affiliated_With_Physician_EmployeeID FOREIGN KEY(Physician) REFERENCES Physician(EmployeeID), 26 | CONSTRAINT fk_Affiliated_With_Department_DepartmentID FOREIGN KEY(Department) REFERENCES Department(DepartmentID), 27 | PRIMARY KEY(Physician, Department) 28 | ); 29 | 30 | DROP TABLE IF EXISTS Procedures; 31 | CREATE TABLE Procedures ( 32 | Code INTEGER PRIMARY KEY NOT NULL, 33 | Name VARCHAR(30) NOT NULL, 34 | Cost REAL NOT NULL 35 | ); 36 | 37 | DROP TABLE IF EXISTS Trained_In; 38 | CREATE TABLE Trained_In ( 39 | Physician INTEGER NOT NULL, 40 | Treatment INTEGER NOT NULL, 41 | CertificationDate DATETIME NOT NULL, 42 | CertificationExpires DATETIME NOT NULL, 43 | CONSTRAINT fk_Trained_In_Physician_EmployeeID FOREIGN KEY(Physician) REFERENCES Physician(EmployeeID), 44 | CONSTRAINT fk_Trained_In_Procedures_Code FOREIGN KEY(Treatment) REFERENCES Procedures(Code), 45 | PRIMARY KEY(Physician, Treatment) 46 | ); 47 | 48 | DROP TABLE IF EXISTS Patient; 49 | CREATE TABLE Patient ( 50 | SSN INTEGER PRIMARY KEY NOT NULL, 51 | Name VARCHAR(30) NOT NULL, 52 | Address VARCHAR(30) NOT NULL, 53 | Phone VARCHAR(30) NOT NULL, 54 | InsuranceID INTEGER NOT NULL, 55 | PCP INTEGER NOT NULL, 56 | CONSTRAINT fk_Patient_Physician_EmployeeID FOREIGN KEY(PCP) REFERENCES Physician(EmployeeID) 57 | ); 58 | 59 | DROP TABLE IF EXISTS Nurse; 60 | CREATE TABLE Nurse ( 61 | EmployeeID INTEGER PRIMARY KEY NOT NULL, 62 | Name VARCHAR(30) NOT NULL, 63 | Position VARCHAR(30) NOT NULL, 64 | Registered BOOLEAN NOT NULL, 65 | SSN INTEGER NOT NULL 66 | ); 67 | 68 | DROP TABLE IF EXISTS Appointment; 69 | CREATE TABLE Appointment ( 70 | AppointmentID INTEGER PRIMARY KEY NOT NULL, 71 | Patient INTEGER NOT NULL, 72 | PrepNurse INTEGER, 73 | Physician INTEGER NOT NULL, 74 | Start DATETIME NOT NULL, 75 | End DATETIME NOT NULL, 76 | ExaminationRoom TEXT NOT NULL, 77 | CONSTRAINT fk_Appointment_Patient_SSN FOREIGN KEY(Patient) REFERENCES Patient(SSN), 78 | CONSTRAINT fk_Appointment_Nurse_EmployeeID FOREIGN KEY(PrepNurse) REFERENCES Nurse(EmployeeID), 79 | CONSTRAINT fk_Appointment_Physician_EmployeeID FOREIGN KEY(Physician) REFERENCES Physician(EmployeeID) 80 | ); 81 | 82 | DROP TABLE IF EXISTS Medication; 83 | CREATE TABLE Medication ( 84 | Code INTEGER PRIMARY KEY NOT NULL, 85 | Name VARCHAR(30) NOT NULL, 86 | Brand VARCHAR(30) NOT NULL, 87 | Description VARCHAR(30) NOT NULL 88 | ); 89 | 90 | 91 | DROP TABLE IF EXISTS Prescribes; 92 | CREATE TABLE Prescribes ( 93 | Physician INTEGER NOT NULL, 94 | Patient INTEGER NOT NULL, 95 | Medication INTEGER NOT NULL, 96 | Date DATETIME NOT NULL, 97 | Appointment INTEGER, 98 | Dose VARCHAR(30) NOT NULL, 99 | PRIMARY KEY(Physician, Patient, Medication, Date), 100 | CONSTRAINT fk_Prescribes_Physician_EmployeeID FOREIGN KEY(Physician) REFERENCES Physician(EmployeeID), 101 | CONSTRAINT fk_Prescribes_Patient_SSN FOREIGN KEY(Patient) REFERENCES Patient(SSN), 102 | CONSTRAINT fk_Prescribes_Medication_Code FOREIGN KEY(Medication) REFERENCES Medication(Code), 103 | CONSTRAINT fk_Prescribes_Appointment_AppointmentID FOREIGN KEY(Appointment) REFERENCES Appointment(AppointmentID) 104 | ); 105 | 106 | DROP TABLE IF EXISTS Block; 107 | CREATE TABLE Block ( 108 | BlockFloor INTEGER NOT NULL, 109 | BlockCode INTEGER NOT NULL, 110 | PRIMARY KEY(BlockFloor, BlockCode) 111 | ); 112 | 113 | DROP TABLE IF EXISTS Room; 114 | CREATE TABLE Room ( 115 | RoomNumber INTEGER PRIMARY KEY NOT NULL, 116 | RoomType VARCHAR(30) NOT NULL, 117 | BlockFloor INTEGER NOT NULL, 118 | BlockCode INTEGER NOT NULL, 119 | Unavailable BOOLEAN NOT NULL, 120 | CONSTRAINT fk_Room_Block_PK FOREIGN KEY(BlockFloor, BlockCode) REFERENCES Block(BlockFloor, BlockCode) 121 | ); 122 | 123 | DROP TABLE IF EXISTS On_Call; 124 | CREATE TABLE On_Call ( 125 | Nurse INTEGER NOT NULL, 126 | BlockFloor INTEGER NOT NULL, 127 | BlockCode INTEGER NOT NULL, 128 | OnCallStart DATETIME NOT NULL, 129 | OnCallEnd DATETIME NOT NULL, 130 | PRIMARY KEY(Nurse, BlockFloor, BlockCode, OnCallStart, OnCallEnd), 131 | CONSTRAINT fk_OnCall_Nurse_EmployeeID FOREIGN KEY(Nurse) REFERENCES Nurse(EmployeeID), 132 | CONSTRAINT fk_OnCall_Block_Floor FOREIGN KEY(BlockFloor, BlockCode) REFERENCES Block(BlockFloor, BlockCode) 133 | ); 134 | 135 | DROP TABLE IF EXISTS Stay; 136 | CREATE TABLE Stay ( 137 | StayID INTEGER PRIMARY KEY NOT NULL, 138 | Patient INTEGER NOT NULL, 139 | Room INTEGER NOT NULL, 140 | StayStart DATETIME NOT NULL, 141 | StayEnd DATETIME NOT NULL, 142 | CONSTRAINT fk_Stay_Patient_SSN FOREIGN KEY(Patient) REFERENCES Patient(SSN), 143 | CONSTRAINT fk_Stay_Room_Number FOREIGN KEY(Room) REFERENCES Room(RoomNumber) 144 | ); 145 | 146 | DROP TABLE IF EXISTS Undergoes; 147 | CREATE TABLE Undergoes ( 148 | Patient INTEGER NOT NULL, 149 | Procedures INTEGER NOT NULL, 150 | Stay INTEGER NOT NULL, 151 | DateUndergoes DATETIME NOT NULL, 152 | Physician INTEGER NOT NULL, 153 | AssistingNurse INTEGER, 154 | PRIMARY KEY(Patient, Procedures, Stay, DateUndergoes), 155 | CONSTRAINT fk_Undergoes_Patient_SSN FOREIGN KEY(Patient) REFERENCES Patient(SSN), 156 | CONSTRAINT fk_Undergoes_Procedures_Code FOREIGN KEY(Procedures) REFERENCES Procedures(Code), 157 | CONSTRAINT fk_Undergoes_Stay_StayID FOREIGN KEY(Stay) REFERENCES Stay(StayID), 158 | CONSTRAINT fk_Undergoes_Physician_EmployeeID FOREIGN KEY(Physician) REFERENCES Physician(EmployeeID), 159 | CONSTRAINT fk_Undergoes_Nurse_EmployeeID FOREIGN KEY(AssistingNurse) REFERENCES Nurse(EmployeeID) 160 | ); 161 | 162 | 163 | CREATE TABLE Physician ( 164 | EmployeeID INTEGER PRIMARY KEY NOT NULL, 165 | Name TEXT NOT NULL, 166 | Position TEXT NOT NULL, 167 | SSN INTEGER NOT NULL 168 | ); 169 | 170 | CREATE TABLE Department ( 171 | DepartmentID INTEGER PRIMARY KEY NOT NULL, 172 | Name TEXT NOT NULL, 173 | Head INTEGER NOT NULL 174 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID) 175 | ); 176 | 177 | CREATE TABLE Affiliated_With ( 178 | Physician INTEGER NOT NULL 179 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID), 180 | Department INTEGER NOT NULL 181 | CONSTRAINT fk_Department_DepartmentID REFERENCES Department(DepartmentID), 182 | PrimaryAffiliation BOOLEAN NOT NULL, 183 | PRIMARY KEY(Physician, Department) 184 | ); 185 | 186 | CREATE TABLE Procedure ( 187 | Code INTEGER PRIMARY KEY NOT NULL, 188 | Name TEXT NOT NULL, 189 | Cost REAL NOT NULL 190 | ); 191 | 192 | CREATE TABLE Trained_In ( 193 | Physician INTEGER NOT NULL 194 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID), 195 | Treatment INTEGER NOT NULL 196 | CONSTRAINT fk_Procedure_Code REFERENCES Procedure(Code), 197 | CertificationDate DATETIME NOT NULL, 198 | CertificationExpires DATETIME NOT NULL, 199 | PRIMARY KEY(Physician, Treatment) 200 | ); 201 | 202 | CREATE TABLE Patient ( 203 | SSN INTEGER PRIMARY KEY NOT NULL, 204 | Name TEXT NOT NULL, 205 | Address TEXT NOT NULL, 206 | Phone TEXT NOT NULL, 207 | InsuranceID INTEGER NOT NULL, 208 | PCP INTEGER NOT NULL 209 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID) 210 | ); 211 | 212 | CREATE TABLE Nurse ( 213 | EmployeeID INTEGER PRIMARY KEY NOT NULL, 214 | Name TEXT NOT NULL, 215 | Position TEXT NOT NULL, 216 | Registered BOOLEAN NOT NULL, 217 | SSN INTEGER NOT NULL 218 | ); 219 | 220 | CREATE TABLE Appointment ( 221 | AppointmentID INTEGER PRIMARY KEY NOT NULL, 222 | Patient INTEGER NOT NULL 223 | CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN), 224 | PrepNurse INTEGER 225 | CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID), 226 | Physician INTEGER NOT NULL 227 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID), 228 | Start DATETIME NOT NULL, 229 | End DATETIME NOT NULL, 230 | ExaminationRoom TEXT NOT NULL 231 | ); 232 | 233 | CREATE TABLE Medication ( 234 | Code INTEGER PRIMARY KEY NOT NULL, 235 | Name TEXT NOT NULL, 236 | Brand TEXT NOT NULL, 237 | Description TEXT NOT NULL 238 | ); 239 | 240 | CREATE TABLE Prescribes ( 241 | Physician INTEGER NOT NULL 242 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID), 243 | Patient INTEGER NOT NULL 244 | CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN), 245 | Medication INTEGER NOT NULL 246 | CONSTRAINT fk_Medication_Code REFERENCES Medication(Code), 247 | Date DATETIME NOT NULL, 248 | Appointment INTEGER 249 | CONSTRAINT fk_Appointment_AppointmentID REFERENCES Appointment(AppointmentID), 250 | Dose TEXT NOT NULL, 251 | PRIMARY KEY(Physician, Patient, Medication, Date) 252 | ); 253 | 254 | CREATE TABLE Block ( 255 | Floor INTEGER NOT NULL, 256 | Code INTEGER NOT NULL, 257 | PRIMARY KEY(Floor, Code) 258 | ); 259 | 260 | CREATE TABLE Room ( 261 | Number INTEGER PRIMARY KEY NOT NULL, 262 | Type TEXT NOT NULL, 263 | BlockFloor INTEGER NOT NULL 264 | CONSTRAINT fk_Block_Floor REFERENCES Block(Floor), 265 | BlockCode INTEGER NOT NULL 266 | CONSTRAINT fk_Block_Code REFERENCES Block(Code), 267 | Unavailable BOOLEAN NOT NULL 268 | ); 269 | 270 | CREATE TABLE On_Call ( 271 | Nurse INTEGER NOT NULL 272 | CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID), 273 | BlockFloor INTEGER NOT NULL 274 | CONSTRAINT fk_Block_Floor REFERENCES Block(Floor), 275 | BlockCode INTEGER NOT NULL 276 | CONSTRAINT fk_Block_Code REFERENCES Block(Code), 277 | Start DATETIME NOT NULL, 278 | End DATETIME NOT NULL, 279 | PRIMARY KEY(Nurse, BlockFloor, BlockCode, Start, End) 280 | ); 281 | 282 | CREATE TABLE Stay ( 283 | StayID INTEGER PRIMARY KEY NOT NULL, 284 | Patient INTEGER NOT NULL 285 | CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN), 286 | Room INTEGER NOT NULL 287 | CONSTRAINT fk_Room_Number REFERENCES Room(Number), 288 | Start DATETIME NOT NULL, 289 | End DATETIME NOT NULL 290 | ); 291 | 292 | CREATE TABLE Undergoes ( 293 | Patient INTEGER NOT NULL 294 | CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN), 295 | Procedure INTEGER NOT NULL 296 | CONSTRAINT fk_Procedure_Code REFERENCES Procedure(Code), 297 | Stay INTEGER NOT NULL 298 | CONSTRAINT fk_Stay_StayID REFERENCES Stay(StayID), 299 | Date DATETIME NOT NULL, 300 | Physician INTEGER NOT NULL 301 | CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID), 302 | AssistingNurse INTEGER 303 | CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID), 304 | PRIMARY KEY(Patient, Procedure, Stay, Date) 305 | ); 306 | Sample dataset 307 | INSERT INTO Physician VALUES(1,'John Dorian','Staff Internist',111111111); 308 | INSERT INTO Physician VALUES(2,'Elliot Reid','Attending Physician',222222222); 309 | INSERT INTO Physician VALUES(3,'Christopher Turk','Surgical Attending Physician',333333333); 310 | INSERT INTO Physician VALUES(4,'Percival Cox','Senior Attending Physician',444444444); 311 | INSERT INTO Physician VALUES(5,'Bob Kelso','Head Chief of Medicine',555555555); 312 | INSERT INTO Physician VALUES(6,'Todd Quinlan','Surgical Attending Physician',666666666); 313 | INSERT INTO Physician VALUES(7,'John Wen','Surgical Attending Physician',777777777); 314 | INSERT INTO Physician VALUES(8,'Keith Dudemeister','MD Resident',888888888); 315 | INSERT INTO Physician VALUES(9,'Molly Clock','Attending Psychiatrist',999999999); 316 | 317 | INSERT INTO Department VALUES(1,'General Medicine',4); 318 | INSERT INTO Department VALUES(2,'Surgery',7); 319 | INSERT INTO Department VALUES(3,'Psychiatry',9); 320 | 321 | INSERT INTO Affiliated_With VALUES(1,1,1); 322 | INSERT INTO Affiliated_With VALUES(2,1,1); 323 | INSERT INTO Affiliated_With VALUES(3,1,0); 324 | INSERT INTO Affiliated_With VALUES(3,2,1); 325 | INSERT INTO Affiliated_With VALUES(4,1,1); 326 | INSERT INTO Affiliated_With VALUES(5,1,1); 327 | INSERT INTO Affiliated_With VALUES(6,2,1); 328 | INSERT INTO Affiliated_With VALUES(7,1,0); 329 | INSERT INTO Affiliated_With VALUES(7,2,1); 330 | INSERT INTO Affiliated_With VALUES(8,1,1); 331 | INSERT INTO Affiliated_With VALUES(9,3,1); 332 | 333 | INSERT INTO Procedure VALUES(1,'Reverse Rhinopodoplasty',1500.0); 334 | INSERT INTO Procedure VALUES(2,'Obtuse Pyloric Recombobulation',3750.0); 335 | INSERT INTO Procedure VALUES(3,'Folded Demiophtalmectomy',4500.0); 336 | INSERT INTO Procedure VALUES(4,'Complete Walletectomy',10000.0); 337 | INSERT INTO Procedure VALUES(5,'Obfuscated Dermogastrotomy',4899.0); 338 | INSERT INTO Procedure VALUES(6,'Reversible Pancreomyoplasty',5600.0); 339 | INSERT INTO Procedure VALUES(7,'Follicular Demiectomy',25.0); 340 | 341 | INSERT INTO Patient VALUES(100000001,'John Smith','42 Foobar Lane','555-0256',68476213,1); 342 | INSERT INTO Patient VALUES(100000002,'Grace Ritchie','37 Snafu Drive','555-0512',36546321,2); 343 | INSERT INTO Patient VALUES(100000003,'Random J. Patient','101 Omgbbq Street','555-1204',65465421,2); 344 | INSERT INTO Patient VALUES(100000004,'Dennis Doe','1100 Foobaz Avenue','555-2048',68421879,3); 345 | 346 | INSERT INTO Nurse VALUES(101,'Carla Espinosa','Head Nurse',1,111111110); 347 | INSERT INTO Nurse VALUES(102,'Laverne Roberts','Nurse',1,222222220); 348 | INSERT INTO Nurse VALUES(103,'Paul Flowers','Nurse',0,333333330); 349 | 350 | INSERT INTO Appointment VALUES(13216584,100000001,101,1,'2008-04-24 10:00','2008-04-24 11:00','A'); 351 | INSERT INTO Appointment VALUES(26548913,100000002,101,2,'2008-04-24 10:00','2008-04-24 11:00','B'); 352 | INSERT INTO Appointment VALUES(36549879,100000001,102,1,'2008-04-25 10:00','2008-04-25 11:00','A'); 353 | INSERT INTO Appointment VALUES(46846589,100000004,103,4,'2008-04-25 10:00','2008-04-25 11:00','B'); 354 | INSERT INTO Appointment VALUES(59871321,100000004,NULL,4,'2008-04-26 10:00','2008-04-26 11:00','C'); 355 | INSERT INTO Appointment VALUES(69879231,100000003,103,2,'2008-04-26 11:00','2008-04-26 12:00','C'); 356 | INSERT INTO Appointment VALUES(76983231,100000001,NULL,3,'2008-04-26 12:00','2008-04-26 13:00','C'); 357 | INSERT INTO Appointment VALUES(86213939,100000004,102,9,'2008-04-27 10:00','2008-04-21 11:00','A'); 358 | INSERT INTO Appointment VALUES(93216548,100000002,101,2,'2008-04-27 10:00','2008-04-27 11:00','B'); 359 | 360 | INSERT INTO Medication VALUES(1,'Procrastin-X','X','N/A'); 361 | INSERT INTO Medication VALUES(2,'Thesisin','Foo Labs','N/A'); 362 | INSERT INTO Medication VALUES(3,'Awakin','Bar Laboratories','N/A'); 363 | INSERT INTO Medication VALUES(4,'Crescavitin','Baz Industries','N/A'); 364 | INSERT INTO Medication VALUES(5,'Melioraurin','Snafu Pharmaceuticals','N/A'); 365 | 366 | INSERT INTO Prescribes VALUES(1,100000001,1,'2008-04-24 10:47',13216584,'5'); 367 | INSERT INTO Prescribes VALUES(9,100000004,2,'2008-04-27 10:53',86213939,'10'); 368 | INSERT INTO Prescribes VALUES(9,100000004,2,'2008-04-30 16:53',NULL,'5'); 369 | 370 | INSERT INTO Block VALUES(1,1); 371 | INSERT INTO Block VALUES(1,2); 372 | INSERT INTO Block VALUES(1,3); 373 | INSERT INTO Block VALUES(2,1); 374 | INSERT INTO Block VALUES(2,2); 375 | INSERT INTO Block VALUES(2,3); 376 | INSERT INTO Block VALUES(3,1); 377 | INSERT INTO Block VALUES(3,2); 378 | INSERT INTO Block VALUES(3,3); 379 | INSERT INTO Block VALUES(4,1); 380 | INSERT INTO Block VALUES(4,2); 381 | INSERT INTO Block VALUES(4,3); 382 | 383 | INSERT INTO Room VALUES(101,'Single',1,1,0); 384 | INSERT INTO Room VALUES(102,'Single',1,1,0); 385 | INSERT INTO Room VALUES(103,'Single',1,1,0); 386 | INSERT INTO Room VALUES(111,'Single',1,2,0); 387 | INSERT INTO Room VALUES(112,'Single',1,2,1); 388 | INSERT INTO Room VALUES(113,'Single',1,2,0); 389 | INSERT INTO Room VALUES(121,'Single',1,3,0); 390 | INSERT INTO Room VALUES(122,'Single',1,3,0); 391 | INSERT INTO Room VALUES(123,'Single',1,3,0); 392 | INSERT INTO Room VALUES(201,'Single',2,1,1); 393 | INSERT INTO Room VALUES(202,'Single',2,1,0); 394 | INSERT INTO Room VALUES(203,'Single',2,1,0); 395 | INSERT INTO Room VALUES(211,'Single',2,2,0); 396 | INSERT INTO Room VALUES(212,'Single',2,2,0); 397 | INSERT INTO Room VALUES(213,'Single',2,2,1); 398 | INSERT INTO Room VALUES(221,'Single',2,3,0); 399 | INSERT INTO Room VALUES(222,'Single',2,3,0); 400 | INSERT INTO Room VALUES(223,'Single',2,3,0); 401 | INSERT INTO Room VALUES(301,'Single',3,1,0); 402 | INSERT INTO Room VALUES(302,'Single',3,1,1); 403 | INSERT INTO Room VALUES(303,'Single',3,1,0); 404 | INSERT INTO Room VALUES(311,'Single',3,2,0); 405 | INSERT INTO Room VALUES(312,'Single',3,2,0); 406 | INSERT INTO Room VALUES(313,'Single',3,2,0); 407 | INSERT INTO Room VALUES(321,'Single',3,3,1); 408 | INSERT INTO Room VALUES(322,'Single',3,3,0); 409 | INSERT INTO Room VALUES(323,'Single',3,3,0); 410 | INSERT INTO Room VALUES(401,'Single',4,1,0); 411 | INSERT INTO Room VALUES(402,'Single',4,1,1); 412 | INSERT INTO Room VALUES(403,'Single',4,1,0); 413 | INSERT INTO Room VALUES(411,'Single',4,2,0); 414 | INSERT INTO Room VALUES(412,'Single',4,2,0); 415 | INSERT INTO Room VALUES(413,'Single',4,2,0); 416 | INSERT INTO Room VALUES(421,'Single',4,3,1); 417 | INSERT INTO Room VALUES(422,'Single',4,3,0); 418 | INSERT INTO Room VALUES(423,'Single',4,3,0); 419 | 420 | INSERT INTO On_Call VALUES(101,1,1,'2008-11-04 11:00','2008-11-04 19:00'); 421 | INSERT INTO On_Call VALUES(101,1,2,'2008-11-04 11:00','2008-11-04 19:00'); 422 | INSERT INTO On_Call VALUES(102,1,3,'2008-11-04 11:00','2008-11-04 19:00'); 423 | INSERT INTO On_Call VALUES(103,1,1,'2008-11-04 19:00','2008-11-05 03:00'); 424 | INSERT INTO On_Call VALUES(103,1,2,'2008-11-04 19:00','2008-11-05 03:00'); 425 | INSERT INTO On_Call VALUES(103,1,3,'2008-11-04 19:00','2008-11-05 03:00'); 426 | 427 | INSERT INTO Stay VALUES(3215,100000001,111,'2008-05-01','2008-05-04'); 428 | INSERT INTO Stay VALUES(3216,100000003,123,'2008-05-03','2008-05-14'); 429 | INSERT INTO Stay VALUES(3217,100000004,112,'2008-05-02','2008-05-03'); 430 | 431 | INSERT INTO Undergoes VALUES(100000001,6,3215,'2008-05-02',3,101); 432 | INSERT INTO Undergoes VALUES(100000001,2,3215,'2008-05-03',7,101); 433 | INSERT INTO Undergoes VALUES(100000004,1,3217,'2008-05-07',3,102); 434 | INSERT INTO Undergoes VALUES(100000004,5,3217,'2008-05-09',6,NULL); 435 | INSERT INTO Undergoes VALUES(100000001,7,3217,'2008-05-10',7,101); 436 | INSERT INTO Undergoes VALUES(100000004,4,3217,'2008-05-13',3,103); 437 | 438 | INSERT INTO Trained_In VALUES(3,1,'2008-01-01','2008-12-31'); 439 | INSERT INTO Trained_In VALUES(3,2,'2008-01-01','2008-12-31'); 440 | INSERT INTO Trained_In VALUES(3,5,'2008-01-01','2008-12-31'); 441 | INSERT INTO Trained_In VALUES(3,6,'2008-01-01','2008-12-31'); 442 | INSERT INTO Trained_In VALUES(3,7,'2008-01-01','2008-12-31'); 443 | INSERT INTO Trained_In VALUES(6,2,'2008-01-01','2008-12-31'); 444 | INSERT INTO Trained_In VALUES(6,5,'2007-01-01','2007-12-31'); 445 | INSERT INTO Trained_In VALUES(6,6,'2008-01-01','2008-12-31'); 446 | INSERT INTO Trained_In VALUES(7,1,'2008-01-01','2008-12-31'); 447 | INSERT INTO Trained_In VALUES(7,2,'2008-01-01','2008-12-31'); 448 | INSERT INTO Trained_In VALUES(7,3,'2008-01-01','2008-12-31'); 449 | INSERT INTO Trained_In VALUES(7,4,'2008-01-01','2008-12-31'); 450 | INSERT INTO Trained_In VALUES(7,5,'2008-01-01','2008-12-31'); 451 | INSERT INTO Trained_In VALUES(7,6,'2008-01-01','2008-12-31'); 452 | INSERT INTO Trained_In VALUES(7,7,'2008-01-01','2008-12-31'); 453 | --------------------------------------------------------------------------------