How to implement a 'WHERE EXISTS' clause in SQL SELECT statements? - sql

I need to make sure that the query listed below is implemented as a nested query with EXISTS/NOT EXISTS clauses.
"Find the first and last name of drivers who performed at least one trip in 2017."
Here is my code so far:
SELECT EMPLOYEE.FNAME, EMPLOYEE.LNAME
FROM EMPLOYEE
WHERE EXISTS
(SELECT ENUM FROM DRIVER WHERE EMPLOYEE.ENUM=DRIVER.ENUM) AND
(SELECT LNUM FROM TRIP WHERE TRIP.LNUM=DRIVER.LNUM AND
YEAR(TDATE)=2017);
I am not sure whether or not it is a syntax error or whether I have entered the wrong values. However, I triple checked all the values and they seem to match the table data.
Below I have listed the tables:
CREATE TABLE TRIP(
TNUM DECIMAL(10) NOT NULL,
LNUM DECIMAL(8) NOT NULL,
REGNUM VARCHAR(10) NOT NULL,
TDATE DATE NOT NULL,
CONSTRAINT TRIP_PKEY PRIMARY KEY (TNUM),
CONSTRAINT TRIP_CKEY UNIQUE (LNUM, REGNUM, TDATE),
CONSTRAINT TRIP_FKEY1 FOREIGN KEY (LNUM) REFERENCES DRIVER(LNUM),
CONSTRAINT TRIP_FKEY2 FOREIGN KEY (REGNUM) REFERENCES TRUCK(REGNUM) );
CREATE TABLE DRIVER(
ENUM DECIMAL(12) NOT NULL,
LNUM DECIMAL(8) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
CONSTRAINT DRIVER_PKEY PRIMARY KEY(ENUM),
CONSTRAINT DRIVER_UNIQUE UNIQUE(LNUM),
CONSTRAINT DRIVER_FKEY FOREIGN KEY(ENUM) REFERENCES EMPLOYEE(ENUM),
CONSTRAINT DRIVER_STATUS CHECK ( STATUS IN ('AVAILABLE', 'BUSY', 'ON
LEAVE')) );
CREATE TABLE EMPLOYEE(
ENUM DECIMAL(12) NOT NULL,
FNAME VARCHAR(50) NOT NULL,
INITIALS VARCHAR(5) NULL,
LNAME VARCHAR(50) NOT NULL,
DOB DATE NULL,
BLDG DECIMAL(3) NOT NULL,
STREET VARCHAR(50) NOT NULL,
SUBURB VARCHAR(50) NOT NULL,
STATE VARCHAR(5) NOT NULL,
ZIPCODE DECIMAL(4) NOT NULL,
CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY(ENUM) );
Below I have listed the table data:
INSERT INTO TRIP VALUES( 1, 10001, 'PKR768', '2015-01-12' );
INSERT INTO TRIP VALUES( 2, 10001, 'SYF777', '2015-02-20' );
INSERT INTO TRIP VALUES( 3, 10001, 'KKK007', '2015-03-12' );
INSERT INTO TRIP VALUES( 4, 10001, 'PKR768', '2015-06-29' );
INSERT INTO TRIP VALUES( 5, 20002, 'PKR768', '2015-01-12' );
INSERT INTO TRIP VALUES( 6, 10002, 'SYF777', '2015-02-20' );
INSERT INTO TRIP VALUES( 7, 30005, 'KKK007', '2015-03-12' );
INSERT INTO TRIP VALUES( 8, 10001, 'PKR768', '2015-01-13' );
INSERT INTO TRIP VALUES( 9, 10002, 'QRT834', '2015-09-17' );
INSERT INTO TRIP VALUES(10, 30005, 'KKK007', '2015-12-15' );
INSERT INTO TRIP VALUES(11, 10003, 'SST005', '2016-01-23' );
INSERT INTO TRIP VALUES(12, 10002, 'PKR768', '2016-03-12' );
INSERT INTO TRIP VALUES(13, 20002, 'QRT834', '2015-04-23' );
INSERT INTO TRIP VALUES(14, 20002, 'PKR008', '2015-04-23' );
INSERT INTO TRIP VALUES(15, 30005, 'PKR768', '2015-05-24' );
INSERT INTO TRIP VALUES(16, 30005, 'SST005', '2014-08-02' );
INSERT INTO TRIP VALUES(17, 20002, 'QRT834', '2014-09-17' );
INSERT INTO TRIP VALUES(18, 10001, 'KKK007', '2014-12-15' );
INSERT INTO TRIP VALUES(19, 30005, 'SST005', '2016-01-23' );
INSERT INTO TRIP VALUES(20, 10003, 'PKR768', '2016-03-12' );
INSERT INTO TRIP VALUES(21, 10001, 'QRT834', '2012-04-23' );
INSERT INTO TRIP VALUES(22, 30005, 'PKR008', '2012-04-23' );
INSERT INTO TRIP VALUES(23, 10003, 'PKR768', '2012-05-25' );
INSERT INTO TRIP VALUES(24, 20002, 'SST005', '2012-08-02' );
INSERT INTO TRIP VALUES(25, 10001, 'PKR768', '2014-01-12' );
INSERT INTO TRIP VALUES(26, 10001, 'SYF777', '2013-02-20' );
INSERT INTO TRIP VALUES(27, 20002, 'KKK007', '2013-03-12' );
INSERT INTO TRIP VALUES(28, 30005, 'PKR768', '2010-06-29' );
INSERT INTO TRIP VALUES(29, 10001, 'QRT834', '2010-09-17' );
INSERT INTO TRIP VALUES(30, 10002, 'KKK007', '2010-12-15' );
INSERT INTO TRIP VALUES(31, 10003, 'SST005', '2010-01-23' );
INSERT INTO TRIP VALUES(32, 20002, 'PKR768', '2010-03-12' );
INSERT INTO TRIP VALUES(33, 30005, 'QRT834', '2003-04-23' );
INSERT INTO TRIP VALUES(34, 30005, 'PKR008', '2004-04-23' );
INSERT INTO TRIP VALUES(35, 10001, 'PKR768', '2017-05-24' );
INSERT INTO DRIVER VALUES( 1, 10001, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 2, 10008, 'ON LEAVE' );
INSERT INTO DRIVER VALUES( 3, 10002, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 4, 10004, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 5, 10003, 'ON LEAVE' );
INSERT INTO DRIVER VALUES( 6, 10012, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 7, 20002, 'BUSY' );
INSERT INTO DRIVER VALUES( 8, 20003, 'BUSY' );
INSERT INTO DRIVER VALUES( 9, 30005, 'BUSY' );
INSERT INTO DRIVER VALUES( 10, 40002, 'BUSY' );
INSERT INTO DRIVER VALUES( 11, 20045, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 12, 20055, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 13, 20065, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 14, 10305, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 15, 10345, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 16, 10705, 'AVAILABLE' );
INSERT INTO DRIVER VALUES( 17, 40005, 'AVAILABLE' );
INSERT INTO EMPLOYEE VALUES( 1, 'John', NULL, 'Smith', NULL, 42,
'Victoria St.', 'Hurstville', 'NSW', 2456 );
INSERT INTO EMPLOYEE VALUES( 2, 'Peter', NULL, 'Taylor', '1970-01-
12', 42, 'Victoria St.', 'Hurstville', 'NSW', 2456 );
INSERT INTO EMPLOYEE VALUES( 3, 'John', NULL, 'Doe', '1966-03-23',
12, 'Station St.', 'Dapto', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 4, 'John', NULL, 'Gray', '1988-05-05',
16, 'Station St.', 'Dapto', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 5, 'Adam', NULL, 'Taylor', '1980-01-01',
42, 'Church St.', 'City', 'NSW', 2300 );
INSERT INTO EMPLOYEE VALUES( 6, 'Michael', NULL, 'Jones', '1975-03-
05', 23, 'Waterloo Ave.', 'Surry Hills', 'NSW', 2502 );
INSERT INTO EMPLOYEE VALUES( 7, 'Frederic', NULL, 'Jones', NULL, 3,
'Victoria St.', 'Redfern', 'NSW', 2420 );
INSERT INTO EMPLOYEE VALUES( 8, 'Peter', NULL, 'O''Brien', '1983-02-
28', 19, 'Lucas Dr.', 'Horsley', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 9, 'John', NULL, 'Lucas', '1966-12-16',
20, 'Huxley St.', 'Horsley', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 10, 'John', NULL, 'Fox', '1975-10-15',
18, 'Victoria St.', 'Hurstville', 'NSW', 2456 );
INSERT INTO EMPLOYEE VALUES( 11, 'Adam', NULL, 'Fox', NULL, 45,
'Victoria St.', 'Hurstville', 'NSW', 2456 );
INSERT INTO EMPLOYEE VALUES( 12, 'Phillip', NULL, ',Cox', '1984-12-
12', 5, 'The Avenue', 'Rockdale', 'NSW', 2300 );
INSERT INTO EMPLOYEE VALUES( 13, 'Andrew', 'K', 'Smith', '1969-04-
04', 42, 'Bambaramba Ave.', 'Pennant Hills', 'NSW', 2556 );
INSERT INTO EMPLOYEE VALUES( 14, 'Andrew', 'R', 'Smith', '1992-04-
01', 67, 'King Cr.', 'Hurstville', 'NSW', 2456 );
INSERT INTO EMPLOYEE VALUES( 15, 'Michael', NULL, 'Potter', '1995-04-
01', 568, 'Bong Bong St.', 'Horsley', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 16, 'Harry', NULL, 'Potter', '1995-04-
01', 568, 'Bong Bong St.', 'Horsley', 'NSW', 2530 );
INSERT INTO EMPLOYEE VALUES( 17, 'James', NULL, 'Bond', NULL, 007,
'Alan Bond St.', 'Perth', 'WA', 6000 );
INSERT INTO EMPLOYEE VALUES( 18, 'Paris', NULL, 'Hilton', NULL, 1,
'Hilton St.', 'Melbourne', 'Vic', 3000 );
INSERT INTO EMPLOYEE VALUES( 19, 'Lady', NULL, 'Gaga', NULL, 3, 'Pork
st.', 'Hobart', 'Tas', 7000 );
INSERT INTO EMPLOYEE VALUES( 20, 'Robin', NULL, 'Hood', NULL, 6,
'Nottingham Pl.', 'Sydney', 'NSW', 2000 );

The correct syntax is:
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
WHERE EXISTS (SELECT 1
FROM DRIVER D
WHERE D.ENUM = E.ENUM
) AND
EXISTS (SELECT 1
FROM TRIP T
WHERE T.LNUM = E.LNUM AND YEAR(T.TDATE) = 2017
);
You need to repeat the EXISTS. In addition:
Use table aliases and qualify all column references. This is a best practice, so get used to it now when you are learning SQL.
The value in the SELECT for an EXISTS is irrelevant. I use 1 because it is easy to type.
Not all databases support YEAR(). The standard syntax is EXTRACT(YEAR FROM T.TDATE). I assume your database does support the function you are using.
I doubt that you really need to compare to DRIVERS (in the real world). Wouldn't a drive on a trip automatically be a "driver"?

Related

insert or update on table violates foreign key constraint "" and duplicate foreign keys

ERROR: insert or update on table "flight" violates foreign key constraint "flight_flight_num_fkey"
DETAIL: Key (flight_num)=(100) is not present in table "booked_on".
drop table if exists CUSTOMER;
drop table if exists BOOKED_ON;
drop table if exists PLANE_TYPE;
drop table if exists EMPLOYEE;
drop table if exists PILOT;
drop table if exists ASSIGNED_TO;
drop table if exists AIRCRAFT;
drop table if exists DEPARTURE;
drop table if exists FLIGHT;
CREATE TABLE CUSTOMER(
Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20) NOT NULL,
Street VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL,
Zip_Code INT NOT NULL,
Phone VARCHAR(20),
PRIMARY KEY (Name) );
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
CREATE TABLE PLANE_TYPE(
Model_no VARCHAR(20) NOT NULL,
Manufacturer VARCHAR(20) NOT NULL,
Unique(Model_no),
PRIMARY KEY (Model_no, Manufacturer) );
CREATE TABLE EMPLOYEE(
employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Salary INT NOT NULL,
PRIMARY KEY (employee_no));
CREATE TABLE PILOT(
Employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
License_Date DATE NOT NULL,
Plane_model_can_fly VARCHAR(20) NOT NULL,
UNIQUE (License_Date),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no));
CREATE TABLE ASSIGNED_TO(
Employee_no INT NOT NULL,
dep_date VARCHAR(20) NOT NULL,
flight_num INT,
PRIMARY KEY (Employee_no),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON(Flight_num)
);
CREATE TABLE AIRCRAFT(
Serial_no VARCHAR(50) ,
Model_no VARCHAR(50),
PRIMARY KEY (Serial_no),
FOREIGN KEY (Model_no) REFERENCES PLANE_TYPE(Model_no)
);
CREATE TABLE DEPARTURE(
Departure_date DATE NOT NULL,
Flight_number INT,
Aircraft_Serial_no VARCHAR(50) ,
Employee_no INT NOT NULL,
PRIMARY KEY(Flight_number),
FOREIGN KEY (Flight_number) REFERENCES BOOKED_ON (Flight_num),
FOREIGN KEY (employee_no) REFERENCES EMPLOYEE(Employee_no),
FOREIGN KEY (Aircraft_Serial_no) REFERENCES AIRCRAFT(Serial_no));
CREATE TABLE FLIGHT(
flight_num INT,
origin VARCHAR(20) NOT NULL,
dest VARCHAR(20) NOT NULL,
dep_time VARCHAR(20) NOT NULL,
arr_time VARCHAR(20) NOT NULL,
PRIMARY KEY(flight_num),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON (Flight_num)
);
/*Populate Customer table*/
INSERT INTO CUSTOMER VALUES
('Rose', 'Parker', '123 Thomas St', 'Toledo', 'OH', 57556, '801-556-2239'),
('Jessica', 'Jones', '234 Pines St', 'Los Angeles', 'CA', 34898, '801-552-2943'),
('John', 'Peters', '345 Star St', 'Raleigh', 'NC', 79999, '801-393-2230'),
('Bryan', 'Brown', '435 Palm St', 'Miami', 'FL', 30533, '801-933-2320'),
('Mark', 'Williams', '348 Andrew St', 'Fort Lauderdale', 'FL', 33318, '801-343-2320'),
('Carol', 'Phillips', '395 Pine St', 'Omaha', 'NE', 88899, '801-323-2320'),
('Madison', 'Parker', '285 Diamond St', 'San Diego', 'CA', 99977, '801-493-2203'),
('Justin', 'Colano', '223 Easy St', 'Tampa', 'FL', 66798, '801-193-2320'),
('Claudia', 'Stevens', '775 Main St', 'Saint Louis', 'MO', 99878, '801-303-2222'),
('Arthur', 'Hooper', '456 Rose St', 'Las Vegas', 'NV', 17878, '313-912-2101'),
('Sergio', 'Ryan', '567 Spruce St', 'Lincoln', 'NE', 87898, '801-228-6729'),
('Julia', 'Maverick', '678 Tulip St', 'Raleigh', 'NC', 79999, NULL),
('Brandon', 'Gordon', '789 First St', 'Miami', 'FL', 30533, NULL);
/*Populate PLANE_TYPE table*/
INSERT INTO PLANE_TYPE VALUES
('B727', 'Boeing'),
('B747', 'Boeing'),
('B757', 'Boeing'),
('DC9', 'MD'),
('DC10', 'MD'),
('A310', 'Airbus'),
('A320', 'Airbus'),
('A330', 'Airbus'),
('DA340', 'Airbus');
/*Populate EMPLOYEE table*/
INSERT INTO EMPLOYEE VALUES
(1001,'John Jones', 90000),
(1002,'Albert Peters',85000),
(1003,'Carl Rowe',115000),
(1004,'Phillips', 25000),
(1005,'William Gates', 50000),
(1006,'Robert Clark', 45000),
(1007,'Joseph Warnock', 50000),
(1008,'Lisa Stuart',75000);
/*Populate FLIGHT table*/
INSERT INTO FLIGHT VALUES
(100, 'SLC', 'BOS', '8:00', '17:50'),
(206, 'DFW', 'STL', '9:00', '11:40'),
(334, 'ORD', 'MIA', '12:00', '14:14'),
(335, 'MIA', 'ORD', '15:00', '17:14'),
(336, 'ORD', 'MIA', '18:00', '20:14'),
(337, 'MIA', 'ORD', '20:30', '23:53'),
(121, 'STL', 'SLC', '7:00', '9:13'),
(122, 'JFK', 'YYV', '16:00', '18:53'),
(330, 'STL', 'YYV', '8:30', '10:19'),
(991, 'BOS', 'ORD', '17:00', '18:22'),
(394, 'DFW', 'MIA', '19:00', '21:30'),
(395, 'MIA', 'DFW', '21:00', '23:43'),
(449, 'CDG', 'DEN', '10:00', '19:29'),
(930, 'YYV', 'DCA', '13:00', '16:10'),
(931, 'DCA', 'YYV', '17:00', '18:10'),
(932, 'DCA', 'YYV', '18:00', '19:10'),
(112, 'DCA', 'DEN', '14:00', '18:07');
/*Populate Pilot table*/
INSERT INTO PILOT VALUES
(1001, 'John Jones', '06/01/1999', 'B727'),
(1001, 'John Jones', '08/14/2000', 'B747'),
(1001, 'John Jones', '04/20/2005', 'DC10'),
(1002, 'Albert Peters', '09/25/2010', 'DC9'),
(1002, 'Albert Peters', '04/20/2005', 'A310'),
(1002, 'Albert Peters', '07/17/2003', 'B757'),
(1002, 'Albert Peters', '05/2/2012', 'A320'),
(1002, 'Albert Peters', '12/01/2015', 'B727'),
(1003, 'Carol Rowe', '01/25/2013', 'A310'),
(1003, 'Carol Rowe', '06/05/2015', 'DC9');
/*Populate Booked_on table*/
INSERT INTO BOOKED_ON VALUES
('Rose Parker','10/31/2018', 100),
('Bryan Brown','10/31/2018', 206),
('Arthur Hooper','10/31/2018', 334),
('Sergio Ryan','10/31/2018', 449),
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
('Mark Williams', '11/01/2018', 100),
('Carol Phillips', '11/01/2018', 100),
('Justin Colano','10/31/2018', 449),
('Claudia Stevens','10/31/2018', 449),
('Julia Maverick','11/01/2018', 991),
('John Peters','11/01/2018', 100);
/*Populate AirCraft table*/
INSERT INTO AIRCRAFT VALUES
('B1110', 'B727'),
('B1325', 'B727'),
('B1088', 'B747'),
('B1419', 'B747'),
('B2244', 'B757'),
('B9377', 'B757'),
('D2101', 'DC9'),
('D2216', 'DC9'),
('D2333', 'DC9'),
('D2429', 'DC9'),
('D2530', 'DC10'),
('A7099', 'A310'),
('A8055', 'A320');
/*Populate DEPARTURE table*/
INSERT INTO DEPARTURE VALUES
('10/31/2018', 100, 'B1110'),
('10/31/2018', 112, NULL),
('10/31/2018', 206, 'D2530'),
('10/31/2018', 334, NULL),
('10/31/2018', 335, NULL),
('10/31/2018', 337, ‘D2216’),
('10/31/2018', 449, NULL),
('11/1/2018', 100, 'D2530'),
('11/1/2018', 112, 'A8055'),
('11/1/2018', 206, NULL),
('11/1/2018', 334, NULL),
('11/1/2018', 395, NULL),
('11/1/2018', 991, 'B1325');
INSERT INTO ASSIGNED_TO VALUES
(1001, '10/31/2018', 100),
(1003, '10/31/2018', 100),
(1004, '10/31/2018', 100),
(1002, '10/31/2018', 206),
(1003, '10/31/2018', 337),
(1004, '10/31/2018', 337),
(1005, '10/31/2018', 337),
(1006, '10/31/2018', 337),
(1001, '11/1/2018', 100),
(1005, '11/1/2018', 100),
(1007, '11/1/2018', 100),
(1003, '11/1/2018', 991),
(1006, '11/1/2018', 991),
(1007, '11/1/2018', 991),
(1002, '11/1/2018', 112),
(1004, '11/1/2018', 112),
(1008, '11/1/2018', 112);
Values will not update due to foreign key and duplicate keys. I attempted to change the create table code. I am unsure if I have to add constraints or change the create table code. Please assist with the code
You insert flight before booke_on, need use go for order of insert.
See new query:
drop table if exists CUSTOMER;
drop table if exists BOOKED_ON;
drop table if exists PLANE_TYPE;
drop table if exists EMPLOYEE;
drop table if exists PILOT;
drop table if exists ASSIGNED_TO;
drop table if exists AIRCRAFT;
drop table if exists DEPARTURE;
drop table if exists FLIGHT;
CREATE TABLE CUSTOMER(
Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20) NOT NULL,
Street VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State VARCHAR(20) NOT NULL,
Zip_Code INT NOT NULL,
Phone VARCHAR(20),
PRIMARY KEY (Name) );
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
CREATE TABLE PLANE_TYPE(
Model_no VARCHAR(20) NOT NULL,
Manufacturer VARCHAR(20) NOT NULL,
Unique(Model_no),
PRIMARY KEY (Model_no, Manufacturer) );
CREATE TABLE EMPLOYEE(
employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
Salary INT NOT NULL,
PRIMARY KEY (employee_no));
CREATE TABLE PILOT(
Employee_no INT NOT NULL,
Name VARCHAR(20) NOT NULL,
License_Date DATE NOT NULL,
Plane_model_can_fly VARCHAR(20) NOT NULL,
UNIQUE (License_Date),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no));
CREATE TABLE ASSIGNED_TO(
Employee_no INT NOT NULL,
dep_date VARCHAR(20) NOT NULL,
flight_num INT,
PRIMARY KEY (Employee_no),
FOREIGN KEY (Employee_no) REFERENCES EMPLOYEE(employee_no),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON(Flight_num)
);
CREATE TABLE AIRCRAFT(
Serial_no VARCHAR(50) ,
Model_no VARCHAR(50),
PRIMARY KEY (Serial_no),
FOREIGN KEY (Model_no) REFERENCES PLANE_TYPE(Model_no)
);
CREATE TABLE DEPARTURE(
Departure_date DATE NOT NULL,
Flight_number INT,
Aircraft_Serial_no VARCHAR(50) ,
Employee_no INT NOT NULL,
PRIMARY KEY(Flight_number),
FOREIGN KEY (Flight_number) REFERENCES BOOKED_ON (Flight_num),
FOREIGN KEY (employee_no) REFERENCES EMPLOYEE(Employee_no),
FOREIGN KEY (Aircraft_Serial_no) REFERENCES AIRCRAFT(Serial_no));
CREATE TABLE FLIGHT(
flight_num INT,
origin VARCHAR(20) NOT NULL,
dest VARCHAR(20) NOT NULL,
dep_time VARCHAR(20) NOT NULL,
arr_time VARCHAR(20) NOT NULL,
PRIMARY KEY(flight_num),
FOREIGN KEY (flight_num) REFERENCES BOOKED_ON (Flight_num)
);
/*Populate Customer table*/
INSERT INTO CUSTOMER VALUES
('Rose', 'Parker', '123 Thomas St', 'Toledo', 'OH', 57556, '801-556-2239'),
('Jessica', 'Jones', '234 Pines St', 'Los Angeles', 'CA', 34898, '801-552-2943'),
('John', 'Peters', '345 Star St', 'Raleigh', 'NC', 79999, '801-393-2230'),
('Bryan', 'Brown', '435 Palm St', 'Miami', 'FL', 30533, '801-933-2320'),
('Mark', 'Williams', '348 Andrew St', 'Fort Lauderdale', 'FL', 33318, '801-343-2320'),
('Carol', 'Phillips', '395 Pine St', 'Omaha', 'NE', 88899, '801-323-2320'),
('Madison', 'Parker', '285 Diamond St', 'San Diego', 'CA', 99977, '801-493-2203'),
('Justin', 'Colano', '223 Easy St', 'Tampa', 'FL', 66798, '801-193-2320'),
('Claudia', 'Stevens', '775 Main St', 'Saint Louis', 'MO', 99878, '801-303-2222'),
('Arthur', 'Hooper', '456 Rose St', 'Las Vegas', 'NV', 17878, '313-912-2101'),
('Sergio', 'Ryan', '567 Spruce St', 'Lincoln', 'NE', 87898, '801-228-6729'),
('Julia', 'Maverick', '678 Tulip St', 'Raleigh', 'NC', 79999, NULL),
('Brandon', 'Gordon', '789 First St', 'Miami', 'FL', 30533, NULL);
/*Populate PLANE_TYPE table*/
INSERT INTO PLANE_TYPE VALUES
('B727', 'Boeing'),
('B747', 'Boeing'),
('B757', 'Boeing'),
('DC9', 'MD'),
('DC10', 'MD'),
('A310', 'Airbus'),
('A320', 'Airbus'),
('A330', 'Airbus'),
('DA340', 'Airbus');
/*Populate EMPLOYEE table*/
INSERT INTO EMPLOYEE VALUES
(1001,'John Jones', 90000),
(1002,'Albert Peters',85000),
(1003,'Carl Rowe',115000),
(1004,'Phillips', 25000),
(1005,'William Gates', 50000),
(1006,'Robert Clark', 45000),
(1007,'Joseph Warnock', 50000),
(1008,'Lisa Stuart',75000);
/*Populate Pilot table*/
INSERT INTO PILOT VALUES
(1001, 'John Jones', '06/01/1999', 'B727'),
(1001, 'John Jones', '08/14/2000', 'B747'),
(1001, 'John Jones', '04/20/2005', 'DC10'),
(1002, 'Albert Peters', '09/25/2010', 'DC9'),
(1002, 'Albert Peters', '04/20/2005', 'A310'),
(1002, 'Albert Peters', '07/17/2003', 'B757'),
(1002, 'Albert Peters', '05/2/2012', 'A320'),
(1002, 'Albert Peters', '12/01/2015', 'B727'),
(1003, 'Carol Rowe', '01/25/2013', 'A310'),
(1003, 'Carol Rowe', '06/05/2015', 'DC9');
/*before inser booked_ontable*/
GO
/*Populate Booked_on table*/
INSERT INTO BOOKED_ON VALUES
('Rose Parker','10/31/2018', 100),
('Bryan Brown','10/31/2018', 206),
('Arthur Hooper','10/31/2018', 334),
('Sergio Ryan','10/31/2018', 449),
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
('Mark Williams', '11/01/2018', 100),
('Carol Phillips', '11/01/2018', 100),
('Justin Colano','10/31/2018', 449),
('Claudia Stevens','10/31/2018', 449),
('Julia Maverick','11/01/2018', 991),
('John Peters','11/01/2018', 100);
/* after insert flight*/
GO
/*Populate FLIGHT table*/
INSERT INTO FLIGHT VALUES
(100, 'SLC', 'BOS', '8:00', '17:50'),
(206, 'DFW', 'STL', '9:00', '11:40'),
(334, 'ORD', 'MIA', '12:00', '14:14'),
(335, 'MIA', 'ORD', '15:00', '17:14'),
(336, 'ORD', 'MIA', '18:00', '20:14'),
(337, 'MIA', 'ORD', '20:30', '23:53'),
(121, 'STL', 'SLC', '7:00', '9:13'),
(122, 'JFK', 'YYV', '16:00', '18:53'),
(330, 'STL', 'YYV', '8:30', '10:19'),
(991, 'BOS', 'ORD', '17:00', '18:22'),
(394, 'DFW', 'MIA', '19:00', '21:30'),
(395, 'MIA', 'DFW', '21:00', '23:43'),
(449, 'CDG', 'DEN', '10:00', '19:29'),
(930, 'YYV', 'DCA', '13:00', '16:10'),
(931, 'DCA', 'YYV', '17:00', '18:10'),
(932, 'DCA', 'YYV', '18:00', '19:10'),
(112, 'DCA', 'DEN', '14:00', '18:07');
/*Populate AirCraft table*/
INSERT INTO AIRCRAFT VALUES
('B1110', 'B727'),
('B1325', 'B727'),
('B1088', 'B747'),
('B1419', 'B747'),
('B2244', 'B757'),
('B9377', 'B757'),
('D2101', 'DC9'),
('D2216', 'DC9'),
('D2333', 'DC9'),
('D2429', 'DC9'),
('D2530', 'DC10'),
('A7099', 'A310'),
('A8055', 'A320');
/*Populate DEPARTURE table*/
INSERT INTO DEPARTURE VALUES
('10/31/2018', 100, 'B1110'),
('10/31/2018', 112, NULL),
('10/31/2018', 206, 'D2530'),
('10/31/2018', 334, NULL),
('10/31/2018', 335, NULL),
('10/31/2018', 337, ‘D2216’),
('10/31/2018', 449, NULL),
('11/1/2018', 100, 'D2530'),
('11/1/2018', 112, 'A8055'),
('11/1/2018', 206, NULL),
('11/1/2018', 334, NULL),
('11/1/2018', 395, NULL),
('11/1/2018', 991, 'B1325');
INSERT INTO ASSIGNED_TO VALUES
(1001, '10/31/2018', 100),
(1003, '10/31/2018', 100),
(1004, '10/31/2018', 100),
(1002, '10/31/2018', 206),
(1003, '10/31/2018', 337),
(1004, '10/31/2018', 337),
(1005, '10/31/2018', 337),
(1006, '10/31/2018', 337),
(1001, '11/1/2018', 100),
(1005, '11/1/2018', 100),
(1007, '11/1/2018', 100),
(1003, '11/1/2018', 991),
(1006, '11/1/2018', 991),
(1007, '11/1/2018', 991),
(1002, '11/1/2018', 112),
(1004, '11/1/2018', 112),
(1008, '11/1/2018', 112);
Something further to add.
In your BOOKED_ON create you specify that flight_num is your primary key
CREATE TABLE BOOKED_ON(
Customer_name VARCHAR(20) NOT NULL,
Departure_Date DATE NOT NULL,
Flight_num INT,
PRIMARY KEY (Flight_num),
FOREIGN KEY (Customer_name) REFERENCES CUSTOMER(Name));
However, in your data you are not entering unique values for flight number into that table. e.g.
('Brandon Gardon','11/01/2018', 991),
('Madison Parker','11/01/2018', 991),
There are a number of confusing relationships in your model, you might want to give the design phase another once over.

To find passengers travelling more than thrice on the same route in PL/SQL

Here I am creating three tables, one for storing the Train Info. Another for holding onto the Passenger info and the other one to hold the ticket Info.
create table T_Train_Info(
route_no integer primary key,
source varchar2(50),
destination varchar2(50)
)
create table T_Pay_Info(
pax_id integer primary key,
pax_name varchar2(50),
dob date,
gender varchar2(5)
)
create table T_Tkt_Info(
pax_id integer,
route_no integer,
journey_date date,
seat_no varchar2(5),
primary key(pax_id, route_no, journey_date)
)
In the Train_Info table, I am inserting two unique routes with the same source and destination as there can be different routes for the same source and destination. And filling the other tables in the same manner. In the ticket table, I am repeating values because I aim to find the passenger travelling thrice on the same route.
insert into T_Train_Info values(1, 'Chennai', 'Pune');
insert into T_Train_Info values(2, 'Chennai', 'Pune');
insert into T_Train_Info values(3, 'Bangalore', 'Kolkata');
insert into T_Tkt_Info values(100, 1, to_date('11/03/2022', 'DD/MM/YYYY'), 22);
insert into T_Tkt_Info values(100, 1, to_date('14/08/2022', 'DD/MM/YYYY'), 23);
insert into T_Tkt_Info values(100, 1, to_date('29/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(102, 3, to_date('22/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 1, to_date('27/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 2, to_date('28/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Pay_Info values(100, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(101, 'B', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(102, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(103, 'D', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(104, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(105, 'A', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
Here's my procedure which keeps returning the error saying 'exact fetch returns more than requested number of rows' at the select statement. What am I doing wrong here?
create or replace procedure pr_pass_route_details(x in T_Train_Info.Source%type, y in T_Train_Info.Destination%type) is
pr_name T_Pay_Info.Pax_Name%type;
begin
for i in (select pax_id from t_tkt_info group by pax_id,route_no having count(*) >=3) loop
select pax_name into pr_name from t_pay_info where pax_id = i.pax_id and T_Train_Info.Source=x and T_Train_Info.Destination=y;
dbms_output.put_line(pr_name);
end loop;
end pr_pass_route_details;
i’m not sure why you’ve written a SP to do this as you can achieve this with a simple query:
SELECT
pax_id,
route_no,
COUNT(journey_date)
FROM T_Tkt_Info
GROUP BY
pax_id,
route_no
HAVING COUNT(journey_date) = 3

ORA-02291 error. Unsure what is causing the problem

Everything runs fine. End part gives an ORA-02291 error. Not sure what would be causing that error, since I thought all the code was running well. This was made in Oracle SQL developer, the error message is ORA-02291. I hope someone can give me an answer. Please help me.
Code:
CREATE TABLE comm_customer
(
Customer_Id int NOT NULL,
Customer_Name VARCHAR(50) NOT NULL,
Address VARCHAR(255) NOT NULL,
Billing_Address VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
phone INT NOT NULL,
primary key(Customer_id)
);
CREATE TABLE comm_orders
(
order_Id INT NOT NULL,
Customer_id INT NOT NULL,
order_date DATE NOT NULL,
amount DECIMAL(5,2) NOT NULL,
Order_status VARCHAR(20) NOT NULL,
primary key(order_id),
FOREIGN KEY (customer_id ) REFERENCES comm_customer(customer_id)
);
CREATE TABLE comm_products
(
product_id INT NOT NULL,
product_name VARCHAR(255) NOT NULL,
product_price decimal(5,2) NOT NULL,
product_quantity decimal(5,2) NOT NULL,
product_status VARCHAR(255) NOT NULL,
customer_id int NOT NULL,
primary key(product_id),
FOREIGN KEY (customer_id ) REFERENCES comm_customer(customer_id)
);
CREATE TABLE comm_shipments
(
shipment_id INT NOT NULL,
order_id INT NOT NULL,
shipment_date DATE NOT NULL,
PRIMARY KEY (shipment_id),
FOREIGN KEY (order_id ) REFERENCES comm_orders(order_id)
);
CREATE TABLE comm_shopping_cart
(
orderdetails_id INT NOT NULL,
order_id INT NOT NULL,
product_id int NOT NULL,
quantity int NOT NULL,
price decimal(5,2),
primary key(orderdetails_id),
FOREIGN KEY (order_id ) REFERENCES comm_orders(order_id),
FOREIGN KEY (product_id ) REFERENCES comm_products(product_id)
);
--For Table Comm_customer
insert into comm_customer values(1011, 'John', '48 Maple Heights Road', '48 Maple Heights Road', 'Toronto', 'Canada', 9988766779);
insert into comm_customer values(1012, 'James', '32 St.Jordan Cressent', '32 St.Jordan Cressent', 'Chennai', 'India', 9988722779);
insert into comm_customer values(1013, 'Anderson', '5 Thornway Street', '#1755 JBS Colony', 'Surat', 'India', 9988123779 );
insert into comm_customer values(1014, 'Jose', '88 Greenbelt Drive', '#1983 ABS Nagar', 'Mumbai', 'India', 9988766885 );
insert into comm_customer values(1015, 'Leo', '#1765 XSX Nagar', '#10993 ACD Nagar', 'Hoshiarpur', 'India', 9955466779 );
--For Table Comm_products:
INSERT into comm_products values(01, 'XI Phone', 250, 4, 'checked out', 1015);
INSERT into comm_products values(02, 'Book', 120, 2, 'not checked out', 1011);
INSERT INTO comm_products values(03, 'Vegetable',100, 5, 'not checked out', 1012);
INSERT into comm_products values(04, 'Biscuits', 250, 1, 'checked out', 1013);
INSERT into comm_products values(05, 'Shampoo', 500, 2, 'not checked out', 1014);
--For table Comm_orders:
insert into comm_orders values(11, 1014, '2020-MAY-01', 500, 'checked out');
insert into comm_orders values(12, 1011, '2019-JUL-18', 140, 'not checked out');
insert into comm_orders values(13, 1013, '2020-JAN-31', 170, 'checked out');
insert into comm_orders values(14, 1012, '2019-FEB-15', 120, ' not checked out');
insert into comm_orders values(15, 1011, '2018-JUN-21', 600, 'checked out');
--For table comm_shippments:
insert into comm_shipments values(001, 13, '2020-FEB-05');
insert into comm_shipments values(002, 15, '2018-JUL-01');
insert into comm_shipments values(003, 12, '2019-07-25');
insert into comm_shipments values(004, 11, '2020-MAY-05');
insert into comm_shipments values(005, 14, '2019-FEB-25');
--For table comm_shopping_cart:
insert into comm_shopping_cart values(701, 01, 11, 4, 250);
insert into comm_shopping_cart values(702, 02, 12, 3, 120);
insert into comm_shopping_cart values(704, 03, 13, 6, 100);
insert into comm_shopping_cart values(703, 04, 14, 2, 250);
insert into comm_shopping_cart values(705, 05, 15, 1, 500);
OK, so you say the statement that is erroring out is this
insert into comm_shopping_cart values(701, 01, 11, 4, 250);
I'd strongly urge you to list the columns in your insert statement. That helps to document things so that someone doesn't have to constantly refer up to the table definition to see what order columns are defined in. And it means that your statements won't suddently fail in the future when you add new columns.
insert into comm_shopping_cart( orderdetails_id,
order_id,
product_id,
quantity,
price)
values(701,
01,
11,
4,
250);
OK, so having done that, the error is that the parent key isn't found. Your actual error message should include the name of the constraint which would tell you which column is the problem. You haven't given us that information so we'll have to figure it out. There are two foreign keys on comm_shopping_cart, the order_id and the product_id. So which one doesn't exist?
You're trying to insert a row with an order_id of 01 (I don't understand why you're putting leading 0's in a numeric column). But you only have order_id values in the comm_orders table between 11 and 15.
You're also trying to insert a row with a product_id of 11. But you only have product_id values between 1 and 5 in your comm_products table. So both foreign key constraints would fail.
My guess is that you intended to specify an order_id of 11 and a product_id of 1 in your insert statement and reversed the order of the columns. Since you didn't include the column list in your insert statement, you made it much harder on yourself to debug the problem because your insert statements aren't self-documenting. Had you specified the column list, you could also have listed the columns in whatever order you wanted so if you wanted to specify the product_id before the order_id you could have.

Error on code in SQL Server

I made this code for SQL Server to select how many times the rider "AsdoVolante" came first in the first semester of 2017. But it does not work. What could be wrong?
SELECT P.Data, Nm.Nome, epp.PosicaoFinal
FROM Prova P, Piloto Nm, EquipaParticipaProva epp
WHERE epp.PosicaoFinal = (SELECT PosicaoFinal FROM EquipaParticipaProva where PosicaoFinal = 1)
HAVING Nm.Nome ='AsdoVolante' AND P.Data BETWEEN '2017-01-01' AND '2017-06-30';
Tables created in SQL Server for the code:
CREATE TABLE Carro(
IdCarro INT NOT NULL,
Marca VARCHAR(20) NOT NULL,
Modelo VARCHAR(50),
PRIMARY KEY (IdCarro));
CREATE TABLE Piloto(
IdPiloto INT NOT NULL,
Nome VARCHAR(20) NOT NULL,
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdPiloto));
CREATE TABLE Equipa(
IdEquipa INT NOT NULL,
Nome VARCHAR(20),
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdEquipa));
CREATE TABLE Patrocinador(
IdPatrocinador INT NOT NULL,
Nome VARCHAR(20)NOT NULL,
Morada VARCHAR(50),
PRIMARY KEY (IdPatrocinador));
CREATE TABLE Prova(
IdProva INT NOT NULL,
Nome VARCHAR(20) NOT NULL,
Local VARCHAR(50) NOT NULL,
Data DATE NOT NULL,
NrVoltas INT NOT NULL,
MelhorTempo TIME,
PRIMARY KEY (IdProva));
CREATE TABLE EquipaParticipaProva(
IdInscricao INT NOT NULL,
IdEquipa INT NOT NULL,
IdCarro INT NOT NULL,
IdPiloto INT NOT NULL,
IdProva INT NOT NULL,
PosicaoFinal INT,
TempoFinal TIME,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
FOREIGN KEY (IdCarro) REFERENCES Carro(IdCarro),
FOREIGN KEY (IdPiloto) REFERENCES Piloto(IdPiloto),
FOREIGN KEY (IdEquipa) REFERENCES Equipa(IdEquipa),
PRIMARY KEY (IdInscricao));
CREATE TABLE Voltas(
IdVolta INT NOT NULL,
IdInscricao INT NOT NULL,
PosicaoRelativa INT NOT NULL,
TempoVolta TIME,
FOREIGN KEY (IdInscricao) REFERENCES EquipaParticipaProva(IdInscricao),
PRIMARY KEY (IdVolta, IdInscricao));
CREATE TABLE PatrocinadorOficial(
IdPatrocinador INT NOT NULL,
IdProva INT NOT NULL,
Valor MONEY NOT NULL,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
PRIMARY KEY (IdPatrocinador, IdProva));
CREATE TABLE PatrocinadorNaoOficial(
IdPatrocinador INT NOT NULL,
IdProva INT NOT NULL,
Valor MONEY NOT NULL,
FOREIGN KEY (IdProva) REFERENCES Prova(IdProva),
PRIMARY KEY (IdPatrocinador, IdProva));
CREATE TABLE Mecanico(
IdMecanico INT NOT NULL,
Nome VARCHAR(20),
Morada VARCHAR(50),
Telefone INT,
PRIMARY KEY (IdMecanico));
CREATE TABLE Afinacao(
IdAfinacao INT NOT NULL,
IdMecanico INT NOT NULL,
IdEquipa INT NOT NULL,
TipoAfinacao VARCHAR(20) NOT NULL,
TempoDespendido TIME,
Data DATE NOT NULL,
FOREIGN KEY (IdEquipa) REFERENCES Equipa(IdEquipa),
FOREIGN KEY (IdMecanico) REFERENCES Mecanico(IdMecanico),
PRIMARY KEY (IdAfinacao));
Inserts created in SQL Server:
INSERT INTO Carro
VALUES (1, 'Nissan', 'Skyline');
INSERT INTO Carro
VALUES (2, 'Subaru', 'Impreza');
INSERT INTO Carro
VALUES (3, 'Mitsubishi', 'Evolution');
INSERT INTO Piloto
VALUES (111, 'Piloto 1', 'Castelo Branco', 111111111);
INSERT INTO Piloto
VALUES (112, 'Piloto 2', 'Castelo Branco', 111111112);
INSERT INTO Piloto
VALUES (113, 'JaGanhou', 'Lisboa', 111111113);
INSERT INTO Piloto
VALUES (114, 'AsdoVolante', 'Lisboa', 111111114);
INSERT INTO Piloto
VALUES (115, 'AsdoPedal', 'Beja', 111111115);
INSERT INTO Piloto
VALUES (116, 'Pelo Nome', 'Santarém', 111111116);
INSERT INTO Equipa
VALUES (001, 'OsAtolados', 'Castelo Branco', 272722123);
INSERT INTO Equipa
VALUES (002, 'Sparco', 'Lisboa', 212312432);
INSERT INTO Equipa
VALUES (003, 'ESTeam', 'Castelo Branco', 272343573);
INSERT INTO Prova
VALUES (010, 'RallycrossCB', 'Castelo Branco', '2017-02-02', 4, '00:04:20');
INSERT INTO Prova
VALUES (11, 'RallyLels', 'Bells', '2017-03-04', 10, '00:10:00');
INSERT INTO Prova
VALUES (12, 'Rally Portugal', 'Fafe', '2017-05-15', 2, '00:15:00');
INSERT INTO Prova
VALUES (13, 'Rally CB', 'Castelo Branco', '2017-01-01', 2, '00:15:00');
INSERT INTO Prova
VALUES (14, 'Rally Norte', 'Porto', '2017-02-02', 2, '00:30:00');
INSERT INTO EquipaParticipaProva
VALUES (441, 002, 3, 113, 010, 1, '00:04:00');
INSERT INTO EquipaParticipaProva
VALUES (442, 1, 2, 111, 11, 1, '00:10:00');
INSERT INTO EquipaParticipaProva
VALUES (443, 3, 1, 112, 10, 1,'00:05:00' );
INSERT INTO EquipaParticipaProva
VALUES (444, 3, 1, 114, 12, 1,'00:15:00' );
INSERT INTO EquipaParticipaProva
VALUES (445, 3, 1, 114, 13, 1,'00:15:00' );
INSERT INTO EquipaParticipaProva
VALUES (446, 3, 2, 114, 14, 5,'01:00:00' );
INSERT INTO Voltas
VALUES (1, 443, 1, '00:01:15');
INSERT INTO Voltas
VALUES (2, 443, 1, '00:01:15');
INSERT INTO Voltas
VALUES (3, 443, 3, '00:01:15');
INSERT INTO Voltas
VALUES (1, 444, 1, '00:07:30');
INSERT INTO Voltas
VALUES (2, 444, 1, '00:07:30');
INSERT INTO Voltas
VALUES (1, 445, 1, '00:07:30');
INSERT INTO Voltas
VALUES (2, 445, 1, '00:07:30');
INSERT INTO Voltas
VALUES (1, 446, 5, '00:30:00');
INSERT INTO Voltas
VALUES (2, 446, 5, '00:30:00');
INSERT INTO Voltas
VALUES (4, 443, 3, '00:01:15');
INSERT INTO Voltas
VALUES (1, 441, 1, '00:01:00' );
INSERT INTO Voltas
VALUES (2, 441, 1, '00:01:20');
INSERT INTO Voltas
VALUES (3, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (4, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (5, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (6, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (7, 441, 1, '00:01:00');
INSERT INTO Voltas
VALUES (8, 441, 1, '00:01:20');
INSERT INTO Voltas
VALUES (9, 441, 1 ,'00:00:30');
INSERT INTO Voltas
VALUES (10, 441, 1, '00:01:30');
INSERT INTO Voltas
VALUES (1, 441, 10, 2, '00:00:45');
INSERT INTO Voltas
VALUES (2, 441, 10, 2, '00:01:00');
INSERT INTO Voltas
VALUES (3, 441, 10, 2, '00:01:00');
INSERT INTO Voltas
VALUES (4, 441, 10, 2, '00:01:15');
INSERT INTO PATROCINADOR
VALUES(921, 'IPCB', 'Av da Talagueira');
INSERT INTO PATROCINADOR
VALUES(922, 'Bells Bar', 'Praceta Qualquer Coisa');
INSERT INTO PATROCINADOR
VALUES(923, 'MonsterEnergy', 'USA');
INSERT INTO PATROCINADOR
VALUES(924, 'Sical', 'Lisboa');
INSERT INTO PATROCINADOROFICIAL
VALUES(923, 010, 10000);
INSERT INTO PATROCINADOROFICIAL
VALUES(921, 11, 8000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(920, 010, 5000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(922, 11, 3000);
INSERT INTO PATROCINADORNAOOFICIAL
VALUES(924, 10, 6000);
INSERT INTO MECANICO
VALUES(333, 'Zacarias','Leiria',933628487);
INSERT INTO MECANICO
VALUES(334, 'Zé', 'Bragança', 963628487);
INSERT INTO MECANICO
VALUES(335, 'Luís', 'Lisboa', 913628487);
INSERT INTO AFINACAO
VALUES(20, 333, 001, 'Travões', '1:00:00', '2017-02-03');
INSERT INTO AFINACAO
VALUES(21, 334, 002, 'Luzes', '00:15:00', '2017-02-03');
INSERT INTO AFINACAO
VALUES(22, 335, 003, 'Travoes', '1:00:00', '2017-02-01');
try this
SELECT P.Data, Nm.Nome, epp.PosicaoFinal
FROM Prova P, Piloto Nm, EquipaParticipaProva epp
WHERE epp.PosicaoFinal **IN** (SELECT PosicaoFinal FROM EquipaParticipaProva where PosicaoFinal = 1)
HAVING Nm.Nome ='AsdoVolante' AND P.Data BETWEEN '2017-01-01' AND '2017-06-30';
It will work for you

Microsoft SQL Server

So I'm working on a SQL and I keep getting errors and I have triple checked it and still haven't come up with a reason as to why the queries aren't showing up correctly. I'm using Microsoft SQL Server. These are the errors that I'm currently getting:
Msg 1767, Level 16, State 0, Line 2
Foreign key 'FK_PatientID' references invalid table 'Patient'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'TreatmentDetails'.
Code:
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Hospital')
DROP DATABASE [Hospital]
GO
CREATE DATABASE [Hospital]
GO
USE Hospital
GO
CREATE TABLE [dbo].[Physician]
(
PhysicianID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Specialty VARCHAR(30) NOT NULL,
GraduationDate DATE NOT NULL,
CONSTRAINT [PK_PhysicianID] PRIMARY KEY (PhysicianID)
);
GO
CREATE TABLE [dbo].[TreatmentDetails]
(
TreatmentID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
StartDateTime DATE NOT NULL,
EndDateTime DATE NULL,
Results VARCHAR(30),
CONSTRAINT [PK_TreatmentID] PRIMARY KEY (TreatmentID),
CONSTRAINT [FK_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
CREATE TABLE [dbo].[Patient]
(
PatientID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
DateOfBirth DATE NOT NULL,
CONSTRAINT [PK_PatientID] PRIMARY KEY (PatientID),
);
GO
CREATE TABLE [dbo].[AdmissionDate]
(
AdmissionID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
AdmissionDate DATE NOT NULL,
DischargeDate DATE NULL,
CONSTRAINT [PK_AdmissionID] PRIMARY KEY (AdmissionID),
CONSTRAINT [FK_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
INSERT INTO TreatmentDetails VALUES (1, 12345, 1234, '2014-4-5', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (2, 12346, 1235, '2013-5-6', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (3, 12347, 1236, '2012-7-8', '2014-9-10', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (4, 12348, 1237, '2011-9-10', '2013-11-12', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (5, 12349, 1238, '2010-11-12', NULL, 'NOT DONE')
INSERT INTO Physician VALUES (12345, 'Will', 'Smith', 'Surgeon', '2014-5-9');
INSERT INTO Physician VALUES (12346, 'Jim', 'Carey', 'Pediatrictian', '2013-2-4');
INSERT INTO Physician VALUES (12347, 'Adam', 'Sandler', 'Immunologist', '2012-6-12');
INSERT INTO Physician VALUES (12348, 'Seth', 'Rogan', 'Neurologist', '2010-9-19');
INSERT INTO Physician VALUES (12349, 'James', 'Bond', 'Dermatologist', '2011-5-2');
INSERT INTO Patient VALUES (1234, 'Christopher', 'Thompson', '1989-7-9');
INSERT INTO Patient VALUES (1235, 'Mac', 'Miller', '1970-9-5');
INSERT INTO Patient VALUES (1236, 'Abraham', 'Lincoln', '1988-1-22');
INSERT INTO Patient VALUES (1237, 'George', 'Washington', '1965-2-8');
INSERT INTO Patient VALUES (1238, 'Franklin', 'Roosevelt', '1992-5-19');
INSERT INTO AdmissionDate VALUES (001, 12345, 1234,'2014-2-9', NULL);
INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-15-9');
INSERT INTO AdmissionDate VALUES (003, 12347, 1236,'2014-3-7', '2014-4-9');
INSERT INTO AdmissionDate VALUES (004, 12348, 1237, '2014-8-6', NULL);
INSERT INTO AdmissionDate VALUES (005, 12349, 1238, '2014-5-5', NULL);
GO
USE Hospital
SELECT * FROM Physician
SELECT * FROM Patient
SELECT * FROM AdmissionDate
SELECT * FROM TreatmentDetails
SELECT Patient.PatientID, Patient.FirstName, Patient.LastName, Patient.DateOfBirth, AdmissionDate.AdmissionDate, AdmissionDate.DischargeDate, Physician.Specialty
FROM Patient, AdmissionDate, Physician
WHERE AdmissionDate IS NOT NULL
ORDER BY Patient.LastName, Patient.FirstName, AdmissionDate.AdmissionDate
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NOT NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
You have incorrect order of creating tables.
You are creating foreign keys with same name in different tables.
You are inserting data in tables in incorrect order.
You have provided incorrect date format. Default format is YYYY-MM-DD
and you provide INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-15-9'); 2014-15-9 this is to change.
Here is working script:
USE master
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Hospital')
DROP DATABASE [Hospital]
GO
CREATE DATABASE [Hospital]
GO
USE Hospital
GO
CREATE TABLE [dbo].[Physician]
(
PhysicianID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
Specialty VARCHAR(30) NOT NULL,
GraduationDate DATE NOT NULL,
CONSTRAINT [PK_PhysicianID] PRIMARY KEY (PhysicianID)
);
GO
CREATE TABLE [dbo].[Patient]
(
PatientID INTEGER NOT NULL,
FirstName VARCHAR(30) NOT NULL,
LastName VARCHAR(30) NOT NULL,
DateOfBirth DATE NOT NULL,
CONSTRAINT [PK_PatientID] PRIMARY KEY (PatientID),
);
GO
CREATE TABLE [dbo].[TreatmentDetails]
(
TreatmentID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
StartDateTime DATE NOT NULL,
EndDateTime DATE NULL,
Results VARCHAR(30),
CONSTRAINT [PK_TreatmentDetails_TreatmentID] PRIMARY KEY (TreatmentID),
CONSTRAINT [FK_TreatmentDetails_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_TreatmentDetails_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
CREATE TABLE [dbo].[AdmissionDate]
(
AdmissionID INTEGER NOT NULL,
PhysicianID INTEGER NOT NULL,
PatientID INTEGER NOT NULL,
AdmissionDate DATE NOT NULL,
DischargeDate DATE NULL,
CONSTRAINT [PK_AdmissionDate_AdmissionID] PRIMARY KEY (AdmissionID),
CONSTRAINT [FK_AdmissionDate_PhysicianID] FOREIGN KEY (PhysicianID) REFERENCES Physician(PhysicianID),
CONSTRAINT [FK_AdmissionDate_PatientID] FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
);
GO
INSERT INTO Physician VALUES (12345, 'Will', 'Smith', 'Surgeon', '2014-5-9');
INSERT INTO Physician VALUES (12346, 'Jim', 'Carey', 'Pediatrictian', '2013-2-4');
INSERT INTO Physician VALUES (12347, 'Adam', 'Sandler', 'Immunologist', '2012-6-12');
INSERT INTO Physician VALUES (12348, 'Seth', 'Rogan', 'Neurologist', '2010-9-19');
INSERT INTO Physician VALUES (12349, 'James', 'Bond', 'Dermatologist', '2011-5-2');
INSERT INTO Patient VALUES (1234, 'Christopher', 'Thompson', '1989-7-9');
INSERT INTO Patient VALUES (1235, 'Mac', 'Miller', '1970-9-5');
INSERT INTO Patient VALUES (1236, 'Abraham', 'Lincoln', '1988-1-22');
INSERT INTO Patient VALUES (1237, 'George', 'Washington', '1965-2-8');
INSERT INTO Patient VALUES (1238, 'Franklin', 'Roosevelt', '1992-5-19');
INSERT INTO TreatmentDetails VALUES (1, 12345, 1234, '2014-4-5', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (2, 12346, 1235, '2013-5-6', NULL, 'NOT DONE')
INSERT INTO TreatmentDetails VALUES (3, 12347, 1236, '2012-7-8', '2014-9-10', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (4, 12348, 1237, '2011-9-10', '2013-11-12', 'Patient finished')
INSERT INTO TreatmentDetails VALUES (5, 12349, 1238, '2010-11-12', NULL, 'NOT DONE')
INSERT INTO AdmissionDate VALUES (001, 12345, 1234,'2014-2-9', NULL);
INSERT INTO AdmissionDate VALUES (002, 12346, 1235, '2014-12-8', '2014-9-15');
INSERT INTO AdmissionDate VALUES (003, 12347, 1236,'2014-3-7', '2014-4-9');
INSERT INTO AdmissionDate VALUES (004, 12348, 1237, '2014-8-6', NULL);
INSERT INTO AdmissionDate VALUES (005, 12349, 1238, '2014-5-5', NULL);
GO
USE Hospital
SELECT * FROM Physician
SELECT * FROM Patient
SELECT * FROM AdmissionDate
SELECT * FROM TreatmentDetails
SELECT Patient.PatientID, Patient.FirstName, Patient.LastName, Patient.DateOfBirth, AdmissionDate.AdmissionDate, AdmissionDate.DischargeDate, Physician.Specialty
FROM Patient, AdmissionDate, Physician
WHERE AdmissionDate IS NOT NULL
ORDER BY Patient.LastName, Patient.FirstName, AdmissionDate.AdmissionDate
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName
SELECT Physician.PhysicianID, Physician.FirstName, Physician.LastName, Patient.PatientID, Patient.FirstName, Patient.LastName, TreatmentDetails.TreatmentID, TreatmentDetails.StartDateTime, TreatmentDetails.EndDateTime, TreatmentDetails.Results
FROM Physician, Patient, TreatmentDetails
WHERE TreatmentDetails.EndDateTime IS NOT NULL
ORDER BY TreatmentDetails.StartDateTime, Physician.LastName, Patient.LastName