Мне нужно убедиться, что приведенный ниже запрос реализован как вложенный запрос с предложениями EXISTS / NOT EXISTS.
"Найти имя и фамилию водителя, который выполнил хотя бы одну поездку в 2017 году."
Вот мой код:
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);
Я не уверен, является ли это синтаксической ошибкой или я ввел неправильные значения.Однако я трижды проверил все значения, и они, кажется, соответствуют данным таблицы.
Ниже я перечислил таблицы:
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) );
Ниже я перечислил данные таблицы:
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 );