How to find if letter exist in string SQL - sql

DROP TABLE IF EXISTS t_z_ksiazka CASCADE;
DROP TABLE IF EXISTS t_a_ksiazki CASCADE;
DROP TABLE IF EXISTS t_ksiazka CASCADE;
DROP TABLE IF EXISTS t_zamowienie CASCADE;
DROP TABLE IF EXISTS t_autor CASCADE;
DROP TABLE IF EXISTS t_wydawca CASCADE;
DROP TABLE IF EXISTS t_klient CASCADE;
DROP TABLE IF EXISTS t_dostawca CASCADE;
CREATE TABLE t_ksiazka(
ISBN int2 PRIMARY KEY,
tytul varchar(40),
wydawca int2,
rok varchar(4),
oprawa varchar(10),
dostawca int2,
cena decimal(4,2),
ilosc int2
);
CREATE TABLE t_autor (
id_autora int2 PRIMARY KEY,
imie varchar(40),
nazwisko varchar(40)
);
CREATE TABLE t_a_ksiazki (
id_autora int2,
ISBN int2
);
ALTER TABLE t_a_ksiazki ADD CONSTRAINT klucz PRIMARY KEY (id_autora, ISBN);
ALTER TABLE t_a_ksiazki ADD CONSTRAINT klucz_od_ksiazki FOREIGN KEY (ISBN)
REFERENCES t_ksiazka(ISBN) ON DELETE CASCADE;
ALTER TABLE t_a_ksiazki ADD CONSTRAINT klucz_od_autora FOREIGN KEY (id_autora)
REFERENCES t_autor(id_autora) ON DELETE CASCADE ;
CREATE TABLE t_wydawca (
id_wydawcy int2 PRIMARY KEY,
nazwa varchar(10),
adres varchar(40)
);
ALTER TABLE t_ksiazka ADD CONSTRAINT klucz_od_wydawcy FOREIGN KEY (wydawca)
REFERENCES t_wydawca(id_wydawcy) ON DELETE CASCADE ;
CREATE TABLE t_zamowienie (
id_zam int2 primary key,
id_klienta int2,
data_Zam DATE,
karta int2,
zrealizowane int2
);
CREATE TABLe t_z_ksiazka (
id_zam int2,
isbn int2,
data_Wys DATE,
ilosc int2
);
CREATE TABLE t_klient(
id_klienta int2 PRIMARY KEY,
imie varchar(30),
nazwisko varchar(40),
ulica varchar(30),
miasto varchar(30),
wojewodztwo varchar(30),
kod varchar(30),
telefon varchar(30)
);
ALTER TABLE t_zamowienie ADD CONSTRAINT klucz_od_klienta FOREIGN KEY(id_klienta) REFERENCES
t_klient(id_klienta) ON DELETE CASCADE;
ALTER TABLE t_z_ksiazka ADD CONSTRAINT klucz_od_zamowienia FOREIGN KEY(id_zam) REFERENCES
t_zamowienie(id_zam) ON DELETE CASCADE;
CREATE TABLE t_dostawca (
id_dostawcy int2 PRIMARY KEY,
nazwa varchar(30),
ulica varchar(30),
miejscowosc varchar(30),
wojewodztwo varchar(40),
kod varchar(15),
telefon varchar(15)
);
ALTER TABLE t_z_ksiazka ADD CONSTRAINT klucz_z_ksiazki FOREIGN KEY(isbn) REFERENCES
t_ksiazka(ISBN) ON DELETE CASCADE;
ALTER TABLE t_ksiazka ADD CONSTRAINT klucz_z_dostawcy FOREIGN KEY(dostawca) REFERENCES
t_dostawca(id_dostawcy) ON DELETE CASCADE;
INSERT into t_autor VALUES (1, 'Joseph', 'Heller');
INSERT into t_autor VALUES (2, 'Patrick', 'Suskind');
INSERT into t_autor VALUES (3, 'Ryszard', 'Kapuscinski');
INSERT into t_autor VALUES (4, 'Milan', 'Kundera');
INSERT into t_autor VALUES (5, 'Pawel', 'Huelle');
INSERT into t_autor VALUES (6, 'Maria', 'Heller');
INSERT into t_autor VALUES (7, 'Patrick', 'Nieznany');
INSERT into t_autor VALUES (8, 'Ryszard', 'Kapuscinski');
INSERT into t_autor VALUES (9, 'Adam', 'Mickiewicz');
INSERT into t_autor VALUES (10, 'Henryk', 'Sienkiewicz');
INSERT into t_autor VALUES (11, 'Jozef', 'Wybicki');
INSERT into t_autor VALUES (12, 'Cyprian', 'Norwid');
INSERT into t_autor VALUES (13, 'Wincenty', 'Witos');
INSERT into t_autor VALUES (14, 'Milan', 'Kundera');
INSERT into t_autor VALUES (15, 'Pawel', 'Wspolczesny');
INSERT into t_autor VALUES (16, 'Ryszard', 'Mazur');
INSERT into t_autor VALUES (17, 'Beata', 'Powstaniec');
INSERT into t_autor VALUES (18, 'Dariusz', 'Port');
INSERT into t_autor VALUES (19, 'Wiktor', 'Porto');
INSERT into t_autor VALUES (20, 'Pawel', 'Wllman');
INSERT into t_autor VALUES (21, 'Maria', 'Kuncewiczowa');
INSERT into t_autor VALUES (22, 'Jan', 'Zamoyski');
INSERT into t_autor VALUES (23, 'Marian', 'Zamoyski');
INSERT into t_autor VALUES (24, 'Adam', 'Zielony');
INSERT into t_autor VALUES (25, 'Henryk', 'Wolski');
INSERT into t_autor VALUES (26, 'Juliusz', 'Cezar');
INSERT into t_autor VALUES (27, 'Maria', 'Konopnicka');
INSERT into t_autor VALUES (28, 'Tadeusz', 'Rozewicz');
INSERT into t_autor VALUES (29, 'Juliusz', 'Slowacki');
INSERT into t_autor VALUES (30, 'Pawel', 'Wierny');
INSERT into t_dostawca VALUES (1, 'Goniec', 'Wiejska', 'B-stok', 'Podlaskie', '15-351', '85-7444-555');
INSERT into t_dostawca VALUES (2, 'UPS', 'sucha', 'Lublin', 'lubelskie', '22-100', '85-7444-555');
INSERT into t_dostawca VALUES (3, 'Konik', 'miejska', 'Opole', 'opolskie', '31-100', '85-7444-555');
INSERT into t_dostawca VALUES (4, 'Pociag', 'nowa', 'Gdansk', 'pomorskie', '10-200', '85-7444-555');
INSERT into t_dostawca VALUES (5, 'Poczta', 'srebrna', 'K-owice', 'slaskie', '41-000', '85-7444-555');
INSERT into t_dostawca VALUES (6, 'Stolica', 'zlota', 'Wa-wa', 'mazowieckie', '00-950', '85-7444-555');
INSERT into t_dostawca VALUES (7, 'Kelner', 'wysockiego', 'Cz-wa', 'slaskie', '42-200', '85-7444-555');
INSERT into t_wydawca VALUES (1, 'Czytelnik', 'W-wa');
INSERT into t_wydawca VALUES (2, 'PIW', 'W-wa');
INSERT into t_wydawca VALUES (3, 'Znak', 'W-wa');
INSERT into t_wydawca VALUES (4, 'Helion', 'Wroclaw');
INSERT into t_wydawca VALUES (5, 'Robomatic', 'Wroclaw');
INSERT into t_wydawca VALUES (6, 'Znak', 'Cz-wa');
INSERT into t_ksiazka VALUES (1, 'Kontrabasista', 1, '1997', 'twarda', 1, 20, 5);
INSERT into t_ksiazka VALUES (2, 'Mercedes Benz', 3, '2001', 'twarda', 2, 30, 5);
INSERT into t_ksiazka VALUES (3, 'Tozsamosc', 2, '1998', 'miekka', 3, 20, 4);
INSERT into t_ksiazka VALUES (4, 'Cesarz', 1, '1978', 'twarda', 4, 25, 2);
INSERT into t_ksiazka VALUES (5, 'Lapidarium', 1, '1995', 'twarda', 5, 35 , 2);
INSERT into t_ksiazka VALUES (6, 'Pan Tadeusz', 4, '1997', 'twarda', 6, 20, 5);
INSERT into t_ksiazka VALUES (7, 'Potop', 5, '2001', 'twarda', 7, 30, 5);
INSERT into t_ksiazka VALUES (8, 'mazurek dabrowskiego', 6, '1998', 'miekka', 1, 20, 4);
INSERT into t_ksiazka VALUES (9, 'fortepian chopina', 6, '1978', 'twarda', 2, 25, 2);
INSERT into t_ksiazka VALUES (10, 'pole dla wszystkich', 5, '1995', 'twarda', 3, 35 , 2);
INSERT into t_ksiazka VALUES (11, 'stol', 4, '1997', 'twarda', 4, 20, 5);
INSERT into t_ksiazka VALUES (12, 'krzeslo', 3, '2001', 'twarda', 5, 30, 5);
INSERT into t_ksiazka VALUES (13, 'drukarka', 2, '1998', 'miekka', 6, 20, 4);
INSERT into t_ksiazka VALUES (14, 'aparaty cyfrowe', 1, '1978', 'twarda', 7, 25, 2);
INSERT into t_ksiazka VALUES (15, 'nibelung', 6, '1995', 'twarda', 1, 35 , 2);
INSERT into t_ksiazka VALUES (16, 'Pan samochodzik', 1, '2007', 'miekka', 1, 20, 5);
INSERT into t_ksiazka VALUES (17, 'Mlode wilki', 3, '2001', 'twarda', 2, 30, 5);
INSERT into t_ksiazka VALUES (18, 'Tosca', 2, '1998', 'miekka', 3, 20, 4);
INSERT into t_ksiazka VALUES (19, 'Winetoo', 1, '1978', 'twarda', 4, 25, 2);
INSERT into t_ksiazka VALUES (20, 'Stawka wieksza niz zycie', 1, '1995', 'twarda', 5, 35 , 2);
INSERT into t_ksiazka VALUES (21, 'Czterej pancerni bez psa', 4, '1997', 'twarda', 6, 20, 5);
INSERT into t_ksiazka VALUES (22, 'Egzorcysta', 5, '2001', 'twarda', 7, 30, 5);
INSERT into t_ksiazka VALUES (23, 'himalaje', 6, '1998', 'miekka', 1, 20, 4);
INSERT into t_ksiazka VALUES (24, 'nibylandia', 6, '1978', 'twarda', 2, 25, 2);
INSERT into t_ksiazka VALUES (25, 'ostatni mohikanin', 5, '1995', 'twarda', 3, 35 , 2);
INSERT into t_ksiazka VALUES (26, 'ostatni most', 4, '1997', 'twarda', 4, 20, 5);
INSERT into t_ksiazka VALUES (27, 'w samo poludnie', 3, '2001', 'twarda', 5, 30, 5);
INSERT into t_ksiazka VALUES (28, 'powrot do przeszlosci', 2, '1998', 'miekka', 6, 20, 4);
INSERT into t_ksiazka VALUES (29, 'samo zycie', 1, '1978', 'twarda', 7, 25, 2);
INSERT into t_ksiazka VALUES (30, 'klan', 6, '1995', 'twarda', 1, 35 , 2);
INSERT into t_ksiazka VALUES (31, 'm jak milosc', 1, '1997', 'twarda', 1, 20, 5);
INSERT into t_ksiazka VALUES (32, 'bez szans', 3, '2001', 'twarda', 2, 30, 5);
INSERT into t_ksiazka VALUES (33, 'ksiega zakazana', 2, '1998', 'miekka', 3, 20, 4);
INSERT into t_ksiazka VALUES (34, 'Byle do przodu', 1, '1978', 'twarda', 4, 25, 2);
INSERT into t_ksiazka VALUES (35, 'Nic na sile', 1, '1995', 'twarda', 5, 35 , 2);
INSERT into t_ksiazka VALUES (36, 'Anakonda', 4, '1997', 'twarda', 6, 20, 5);
INSERT into t_ksiazka VALUES (37, 'amok', 5, '2001', 'twarda', 7, 30, 5);
INSERT into t_ksiazka VALUES (38, 'koko koko euro spoko', 6, '1998', 'miekka', 1, 20, 4);
INSERT into t_ksiazka VALUES (39, 'na ludowo', 6, '1978','twarda', 2, 25, 2);
INSERT into t_ksiazka VALUES (40, 'K-11', 5, '1995', 'twarda', 3, 35 , 2);
INSERT into t_ksiazka VALUES (41, 'Szczyt', 4, '1997', 'twarda', 4, 20, 5);
INSERT into t_ksiazka VALUES (42, 'piknik pod wiszaca skala', 3, '2001', 'twarda', 5, 30, 5);
INSERT into t_ksiazka VALUES (43, 'killer', 2, '1998', 'miekka', 6, 20, 4);
INSERT into t_ksiazka VALUES (44, 'madagaskar', 1, '1978', 'twarda', 7, 25, 2);
INSERT into t_ksiazka VALUES (45, 'niebo', 6, '1995', 'twarda', 1, 35 , 2);
INSERT into t_a_ksiazki VALUES(2, 1);
INSERT into t_a_ksiazki VALUES(5, 2);
INSERT into t_a_ksiazki VALUES(4, 3);
INSERT into t_a_ksiazki VALUES(3, 4);
INSERT into t_a_ksiazki VALUES(2, 5);
INSERT into t_a_ksiazki VALUES(9, 6);
INSERT into t_a_ksiazki VALUES(10, 7);
INSERT into t_a_ksiazki VALUES(11, 8);
INSERT into t_a_ksiazki VALUES(12, 9);
INSERT into t_a_ksiazki VALUES(5, 10);
INSERT into t_a_ksiazki VALUES(4, 11);
INSERT into t_a_ksiazki VALUES(3, 12);
INSERT into t_a_ksiazki VALUES(2, 13);
INSERT into t_a_ksiazki VALUES(5, 14);
INSERT into t_a_ksiazki VALUES(4, 15);
INSERT into t_a_ksiazki VALUES(2, 31);
INSERT into t_a_ksiazki VALUES(5, 32);
INSERT into t_a_ksiazki VALUES(4, 33);
INSERT into t_a_ksiazki VALUES(15, 34);
INSERT into t_a_ksiazki VALUES(16, 35);
INSERT into t_a_ksiazki VALUES(17, 36);
INSERT into t_a_ksiazki VALUES(19, 37);
INSERT into t_a_ksiazki VALUES(19, 38);
INSERT into t_a_ksiazki VALUES(11, 39);
INSERT into t_a_ksiazki VALUES(25, 40);
INSERT into t_a_ksiazki VALUES(24, 41);
INSERT into t_a_ksiazki VALUES(23, 42);
INSERT into t_a_ksiazki VALUES(22, 43);
INSERT into t_a_ksiazki VALUES(25, 44);
INSERT into t_a_ksiazki VALUES(24, 45);
INSERT into t_a_ksiazki VALUES(22, 16);
INSERT into t_a_ksiazki VALUES(25, 17);
INSERT into t_a_ksiazki VALUES(14, 18);
INSERT into t_a_ksiazki VALUES(13, 19);
INSERT into t_a_ksiazki VALUES(12, 20);
INSERT into t_a_ksiazki VALUES(19, 21);
INSERT into t_a_ksiazki VALUES(30, 22);
INSERT into t_a_ksiazki VALUES(21, 23);
INSERT into t_a_ksiazki VALUES(12, 24);
INSERT into t_a_ksiazki VALUES(15, 25);
INSERT into t_a_ksiazki VALUES(14, 26);
INSERT into t_a_ksiazki VALUES(23, 27);
INSERT into t_a_ksiazki VALUES(22, 28);
INSERT into t_a_ksiazki VALUES(15, 29);
INSERT into t_a_ksiazki VALUES(14, 30);
INSERT into t_klient VALUES (1, 'Jan', 'Kowalski', 'Wiejska', 'W-wa', 'mazowieckie', '00-480', '624-45-56');
INSERT into t_klient VALUES (2, 'Tadeusz', 'Malinowski', 'Nowy Swiat', 'W-wa', 'mazowieckie', '00-480', '624-42-33');
INSERT into t_klient VALUES (3, 'Krystyna', 'Torbicka', 'Krakowska', 'W-wa', 'mazowieckie', '00-480', '624-22-11');
INSERT into t_klient VALUES (4, 'Anna', 'Marzec', 'Suraska', 'B-stok', 'podlaskie', '15-333', '744-34-34');
INSERT into t_klient VALUES (5, 'Adam', 'Kepinski', 'Lipowa', 'B-stok', 'podlaskie', '15-356', '756-34-33');
INSERT into t_zamowienie VALUES(1, 1, '2007-01-10', 1,1);
INSERT into t_zamowienie VALUES(2, 2, '2004-01-10', 1,0);
INSERT into t_zamowienie VALUES(3, 3, '2003-01-10', 0,0);
INSERT into t_zamowienie VALUES(4, 2, '2002-01-11', 0,0);
INSERT into t_zamowienie VALUES(5, 4, '2001-01-11', 1,1);
INSERT into t_zamowienie VALUES(6, 5, '2008-01-11', 1,1);
INSERT into t_zamowienie VALUES(7, 4, '2007-10-12', 1,1);
INSERT into t_zamowienie VALUES(8, 4, '2010-01-12', 1,0);
INSERT into t_zamowienie VALUES(9, 3, '2011-01-12', 0,0);
INSERT into t_zamowienie VALUES(10, 5, '2012-01-12', 0,0);
INSERT into t_zamowienie VALUES(11, 4, '2012-04-12', 1,1);
INSERT into t_zamowienie VALUES(12, 1, '2012-05-12', 1,1);
INSERT into t_z_ksiazka VALUES(1, 1, '2008-01-11', 1);
INSERT into t_z_ksiazka VALUES(1, 2, '2008-01-11', 1);
INSERT into t_z_ksiazka VALUES(1, 3, '2008-01-11', 1);
INSERT into t_z_ksiazka VALUES(2, 3, '2004-01-11', 1);
INSERT into t_z_ksiazka VALUES(2, 4, '2004-01-11', 2);
INSERT into t_z_ksiazka VALUES(3, 1, '2003-01-11', 1);
INSERT into t_z_ksiazka VALUES(4, 2, '2002-01-12', 1);
INSERT into t_z_ksiazka VALUES(4, 5, '2002-01-12', 2);
INSERT into t_z_ksiazka VALUES(5, 5, '2001-01-12', 1);
INSERT into t_z_ksiazka VALUES(5, 4, '2001-01-12', 1);
INSERT into t_z_ksiazka VALUES(6, 15, '2008-01-12', 2);
INSERT into t_z_ksiazka VALUES(7, 11, '2007-01-12', 1);
INSERT into t_z_ksiazka VALUES(8, 12, '2010-01-13', 1);
INSERT into t_z_ksiazka VALUES(9, 13, '2011-01-13', 1);
INSERT into t_z_ksiazka VALUES(9, 3, '2011-01-13', 1);
INSERT into t_z_ksiazka VALUES(9, 4, '2011-01-13', 2);
INSERT into t_z_ksiazka VALUES(10, 10, '2012-01-13', 1);
INSERT into t_z_ksiazka VALUES(11, 7, '2012-04-13', 1);
INSERT into t_z_ksiazka VALUES(11, 5, '2012-04-13', 2);
INSERT into t_z_ksiazka VALUES(12, 5, '2012-05-13', 1);
INSERT into t_z_ksiazka VALUES(12, 8, '2012-05-13', 1);
INSERT into t_z_ksiazka VALUES(12, 9, '2012-05-13', 2);
Search for the name of those authors whose last name is anywhere the same letter as the first letter of the surname author with ID 26.
I was thinking to create a subquery and I come up with :
This return first letter:
select substring(nazwisko,1,1) from t_autor where id_autora=26
select imie from t_autor where nazwisko like ???
how to connect this ? in a normal scenario it would be like '%letter%' but I'm going to get letter after subquery and I have no idea how to get it to work

You have the right idea with the subquery, and to getting the letter to use with LIKE can be accomplished with CONCAT: (as one possible way)
select substring(nazwisko,1,1) from t_autor where id_autora=26;
Gives:
Then using CONCAT as part of the subquery: (I added id_autora to show the reference)
select imie, id_autora from t_autor where nazwisko like (
select CONCAT(substring(nazwisko,1,1), '%') from t_autor where id_autora=26
);
-- The concat has a trailing % for names that start with the letter.
Gives:
As another example:
select substring(nazwisko,1,1) from t_autor where id_autora=20;
Gives:
Then used within the subquery:
select imie, id_autora from t_autor where nazwisko like (
select CONCAT(substring(nazwisko,1,1), '%') from t_autor where id_autora=20
);
Gives:
Here's a DB fiddle example. The query examples used above are at the bottom of the page. As Nick mentioned, having all the other tables was more than enough, so the fiddle only uses table t_autor.

Related

SQL Display every class

Can someone assist me with my Oracle SQL joins, something is missing.
The select statement below lists every Student and the Class Date but I want to show every Class for each Student, even if the Student took the Class or not.
Kim, Brandy and Trina, Brandy should have every class name listed with no Class Date since they have not took any class yet.
Green, Robert took 3 Classes and did not take 2 Classes, the 2 Classes should be listed also with no Class Date since he did not take them.
The insert statements and create tables are listed. Let me know if you have any questions. Thanks
SELECT VW.STUDENT_NAME,
VW.EMPLE_NO,
CN.PK_CLASS_NAME_ID,
CN.CLASS_NAME,
DP.CLASS_DATE
FROM EMPLOYEE_NAME VW
LEFT JOIN DEMO_PRODUCT_INFO_NEW DP ON DP.FK_CO_EMPL_ID = VW.EMPLE_NO
LEFT JOIN TBL_CLASS_NAME_NEW CN
ON CN.PK_CLASS_NAME_ID = DP.FK_CLASS_NAME_ID
ORDER BY STUDENT_NAME ASC;
CREATE TABLE TBL_CLASS_NAME_NEW
(
PK_CLASS_NAME_ID INTEGER,
CLASS_NAME VARCHAR2(75 BYTE)
);
Insert into TBL_CLASS_NAME_NEW
(PK_CLASS_NAME_ID, CLASS_NAME)
Values
(1, 'CPR');
Insert into TBL_CLASS_NAME_NEW
(PK_CLASS_NAME_ID, CLASS_NAME)
Values
(3, 'ETHICS');
Insert into TBL_CLASS_NAME_NEW
(PK_CLASS_NAME_ID, CLASS_NAME)
Values
(4, 'HARRASEMENT');
Insert into TBL_CLASS_NAME_NEW
(PK_CLASS_NAME_ID, CLASS_NAME)
Values
(5, 'DEFENSIVE TEST');
Insert into TBL_CLASS_NAME_NEW
(PK_CLASS_NAME_ID, CLASS_NAME)
Values
(2, 'RANGE');
COMMIT;
CREATE TABLE DEMO_PRODUCT_INFO_NEW
(
PRODUCT_ID NUMBER NOT NULL,
FK_CO_EMPL_ID NUMBER,
FK_CLASS_NAME_ID NUMBER,
CLASS_DATE DATE
);
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(4, 4, 1, TO_DATE('7/18/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(22, 4, 1, TO_DATE('7/25/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(18, 4, 4, TO_DATE('7/18/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(21, 4, 3, TO_DATE('7/4/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(32, 22, 2, TO_DATE('8/15/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(34, 22, 1, TO_DATE('8/29/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(35, 22, 1, TO_DATE('7/4/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(46, 18, 4, TO_DATE('7/4/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(43, 18, 5, TO_DATE('7/11/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(45, 4, 2, TO_DATE('7/4/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(48, 4, 5, TO_DATE('7/11/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(51, 22, 3, TO_DATE('7/4/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(52, 18, 3, TO_DATE('7/18/2021', 'MM/DD/YYYY'));
Insert into DEMO_PRODUCT_INFO_NEW
(PRODUCT_ID, FK_CO_EMPL_ID, FK_CLASS_NAME_ID, CLASS_DATE)
Values
(5, 4, 2, TO_DATE('7/25/2021', 'MM/DD/YYYY'));
COMMIT;
CREATE TABLE EMPLOYEE_NAME
(
EMPLE_NO INTEGER,
STUDENT_NAME VARCHAR2 (100 BYTE),
LAST_NAME VARCHAR2 (40 BYTE),
FIRST_NAME VARCHAR2 (40 BYTE)
);
Insert into EMPLOYEE_NAME
(EMPLE_NO, STUDENT_NAME, LAST_NAME, FIRST_NAME)
Values
(4, 'WENDY, FRANK', 'FRANK', 'WENDY');
Insert into EMPLOYEE_NAME
(EMPLE_NO, STUDENT_NAME, LAST_NAME, FIRST_NAME)
Values
(22, 'JOHN, JAMES', 'JAMES', 'JOHN');
Insert into EMPLOYEE_NAME
(EMPLE_NO, STUDENT_NAME, LAST_NAME, FIRST_NAME)
Values
(18, 'GREEN, ROBERT', 'ROBERT', 'GREEN');
Insert into EMPLOYEE_NAME
(EMPLE_NO, STUDENT_NAME, LAST_NAME, FIRST_NAME)
Values
(21, 'KIM, BRANDY', 'BRANDY', 'KIM');
Insert into EMPLOYEE_NAME
(EMPLE_NO, STUDENT_NAME, LAST_NAME, FIRST_NAME)
Values
(32, 'TRINA, JAMIE', 'JAMIE', 'TRINA');
This is how the following query works
T1: Lists all the courses and students that the student can choose.
T2: Lists all the courses and students that the student has chosen
Then T1 goes to T2 LEFT JOIN with equal number of PK_CLASS_NAME_ID and EMPLE_NO
SELECT
T1.STUDENT_NAME,
T1.EMPLE_NO,
T1.PK_CLASS_NAME_ID,
T1.CLASS_NAME,
T2.CLASS_DATE
FROM
(
SELECT VW.STUDENT_NAME,
VW.EMPLE_NO,
CN.PK_CLASS_NAME_ID,
CN.CLASS_NAME
FROM TBL_CLASS_NAME_NEW CN
JOIN EMPLOYEE_NAME VW
ON 1=1
GROUP BY VW.STUDENT_NAME,VW.EMPLE_NO, CN.PK_CLASS_NAME_ID, CN.CLASS_NAME
) T1
LEFT JOIN
(
SELECT VW.STUDENT_NAME,
VW.EMPLE_NO,
CN.PK_CLASS_NAME_ID,
CN.CLASS_NAME,
DP.CLASS_DATE
FROM TBL_CLASS_NAME_NEW CN
JOIN DEMO_PRODUCT_INFO_NEW DP
ON CN.PK_CLASS_NAME_ID = DP.FK_CLASS_NAME_ID
JOIN EMPLOYEE_NAME VW
ON DP.FK_CO_EMPL_ID = VW.EMPLE_NO
GROUP BY VW.STUDENT_NAME,VW.EMPLE_NO,CN.PK_CLASS_NAME_ID, CN.CLASS_NAME,DP.CLASS_DATE
) T2 ON T1.EMPLE_NO = T2.EMPLE_NO ANd T1.PK_CLASS_NAME_ID = T2.PK_CLASS_NAME_ID
I got the output in SQL, but it doesn't matter, it works in Oracle.

How can I extract rows that does't have a specific value , referring to another table?

I have two tables:
FIRST TABLE:
CREATE TABLE TABLE_MENU
(ID INT IDENTITY PRIMARY KEY,
NAME_MENU VARCHAR(30),
TYPE_OF_MENU VARCHAR(30))
INSERT INTO TABLE_MENU (NAME_MENU, TYPE_OF_MENU) VALUES
('Menu 1','Dinner'),
('Menu 2','Dinner'),
('Menu 3','Lunch'),
('Menu 4','Dinner'),
('Menu 5','Dinner'),
('Menu 6','Lunch'),
('Menu 7','Lunch'),
('Menu 8','Lunch'),
('Menu 9','Lunch'),
('Menu 10','Lunch');
SECOND TABLE:
CREATE TABLE TABLE_DISHES
(ID INT IDENTITY PRIMARY KEY,
ID_MENU INT FOREIGN KEY REFERENCES TABLE_MENU(ID),
NAME_DISH VARCHAR(30),
TYPE_OF_DISH VARCHAR(30))
INSERT INTO TABLE_DISHES (ID_MENU, NAME_DISH, TYPE_OF_DISH) VALUES
(1, 'Salad','Vegetable'),
(1, 'Bacon recipes', 'Meat'),
(1, 'Natural Water', 'Drink'),
(2, 'Tomatoes', 'Vegetable'),
(2, 'Barracuda', 'Fish'),
(2, 'White Wine', 'Drink'),
(3, 'Coca Cola', 'Drink'),
(3, 'Beef recipes', 'Meat'),
(4, 'Tuna', 'Fish'),
(4, 'Sparkling Water','Drink'),
(5, 'Carrots', 'Vegetable'),
(5, 'Chicken recipes', 'Meat'),
(6, 'Corn','Vegetable'),
(6, 'Lamb recipes', 'Meat'),
(6, 'Sprite','Drink'),
(7, 'Mix Salad','Vegetable'),
(8, 'Spinach','Vegetable'),
(8, 'Duck recipes', 'Meat'),
(8, 'Fanta', 'Drink'),
(9, 'Jellyfish', 'Fish'),
(10, 'Gammon recipes','Meat'),
(10, 'Beer','Drink');
What would be my query if I want to extract from the Table_Menu all the Dinner Menus that HAVE NOT vegetable dishes?
In this case my expected output should be: Menu 3, Menu 4, Menu 9, and Menu 10.
Thank you!
You can go for simple NOT EXISTS query.
SELECT * FROM Table_Manu as om
WHERE NOT EXISTS (
SELECT 1 FROM Table_Dishes
WHERE id_menu = om.id_menu
and type_of_dish = 'V')
Except should work for you.
SELECT ID
FROM Type_of_Menu
EXCEPT
SELECT ID_menu
FROM Table_Dishes
WHERE Type_of_Dish = 'V'

Unknown column in where clause?

My code:
drop table if exists HSstudents;
create table HSstudents
(
HSsID int,
vNAAM text,
aNAAM text,
LT int,
GM float
);
insert into HSstudents values (1, 'Thomas', 'Jansen', 18, 7.0);
insert into HSstudents values (2, 'Jesse', 'Bakker', 19, 6.5);
insert into HSstudents values (3, 'Tom', 'Smit', 20, 7.1);
insert into HSstudents values (4, 'Jelle', 'Visser', 17, 9.6);
insert into HSstudents values (5, 'Sem', 'Dekker', 17, 8.1);
insert into HSstudents values (6, 'Anna', 'Peters', 18, 6.8);
insert into HSstudents values (7, 'Michelle', 'Hendriks', 19, 8.2);
insert into HSstudents values (8, 'Senna', 'Mulder', 20, 5.9);
insert into HSstudents values (9, 'Sven', 'Linden', 21, 6.0);
insert into HSstudents values (10, 'Ilse', 'Jacobs', 21, 7.5);
insert into HSstudents values (11, 'Harm', 'Schouten', 19, 7.0);
insert into HSstudents values (12, 'Emma', 'Dijkstra', 18, 8.1);
drop table if exists students;
create table students
(
sID int,
vNAAM text,
aNAAM text,
LT int
);
insert into students values (1, 'Thomas', 'Jansen', 18);
insert into students values (2, 'Jesse', 'Bakker', 19);
insert into students values (3, 'Tom', 'Smit', 20);
insert into students values (4, 'Jelle', 'Visser', 17);
insert into students values (5, 'Sem', 'Dekker', 17);
insert into students values (6, 'Anna', 'Peters', 18);
insert into students values (7, 'Michelle', 'Hendriks', 19);
insert into students values (8, 'Senna', 'Mulder', 20);
insert into students values (9, 'Sven', 'Linden', 21);
insert into students values (10, 'Ilse', 'Jacobs', 21);
insert into students values (11, 'Harm', 'Schouten', 19);
insert into students values (12, 'Emma', 'Dijkstra', 18);
drop table if exists applications;
create table applications
(
sID int,
aPROV text,
sPROV text,
taal text
);
insert into applications values (1, 'Overijssel', 'Drenthe', 'HTML');
insert into applications values (2, 'Gelderland', 'Overijssel', 'CSS');
insert into applications values (3, 'Groningen', 'Flevoland', 'CSS');
insert into applications values (4, 'Overijssel', 'Zuid-Holland', 'SQL');
insert into applications values (5, 'Noord-Holland', 'Drenthe', 'C#');
insert into applications values (6, 'Flevoland', 'Groningen', 'C#');
insert into applications values (7, 'Limburg', 'Groningen', 'JAVA');
insert into applications values (8, 'Limburg', 'Limburg', 'JAVASCRIPT');
insert into applications values (9, 'Drenthe', 'Noord-Brabant', 'CSS');
insert into applications values (10, 'Drenthe', 'Zeeland', 'Python');
insert into applications values (11, 'Zuid-Holland', 'Friesland', 'C++');
insert into applications values (12, 'Zeeland', 'Friesland', 'JAVA');
select
S.sID, S.vNAAM, S.aNAAM, S.LT, aPROV, sPROV, taal
from
HSstudents HS, students S, applications A
where
HSstudents.HSsID = students.sID
This results in an error
Code: 1054. Unknown column 'HSstudents.HSsID' in 'where clause'
How? Shouldn't it just work?
WHERE clause should follow the remane on the FROM clause:
where HS.HSsID = S.sID

TSQL - Find rows matching AND / OR filters

I am trying to find the best way to do this within SQL (or whether SQL is the recommended route to take at all)
I have a couple tables, with a structure as explained below (de-normalized for explaining ease). One is a table of employees with their "tag" definitions. The second is a table of employee groups, along with the criteria for the group.
Employee Table
EmployeeId | Tags
-----------------------------
1 | Country|US
1 | EmployeeType|FullTime
2 | Country|CA
2 | EmployeeType|FullTime
3 | Country|BR
3 | EmployeeType|FullTime
4 | Country|US
4 | EmployeeType|FullTime
4 | Language|Spanish
Group Table
GroupId | Tags | OperatorGroup
---------------------------------------------------------------------------------------------
1 | Country|US | D68BF531-FD9B-4A0E-886E-7420BDFFE380
1 | Country|CA | D68BF531-FD9B-4A0E-886E-7420BDFFE380
1 | EmployeeType|FullTime | 0B97D792-7659-42EF-8036-D19B1A0D5197
2 | Country|BR | CE61BC17-2567-4B02-AE5E-F5EA40265EE7
2 | EmployeeType|FullTime | 6A3418F6-C4FD-40E2-97B3-0F9E87759939
3 | Country|US | BF3DAC05-8131-463F-976E-28099628842D
3 | EmployeeType|FullTime | D63CEDC2-D15F-4E05-B3B9-0109121B3C75
3 | Language|Spanish | 3AA247D1-3779-48BD-8A2C-B9C8A084E32D
In the above table "Group", OperatorGroup determines the condition / operator for that group. Within the same GroupId, I use the OR operator within the same OperatorGroup, and the AND operator otherwise. For GroupId == 1, both Country|US and Country|CA has the same OperatorGroup, hence would be joined by an OR clause, in words, "You will match GroupId = 1 if your country is (US OR CA) AND you are a full time employee"
The result that I am looking for is the groups that the employee is part of. Based on the above data, I would expect the below
EmployeeGroups Result
EmployeeId | GroupId
------------------------------
1 | 1
2 | 1
3 | 2
4 | 1
4 | 3
Is a SQL-only solution (TSQL - SQL Server 2012) too ambitious? Any pointers either way would be much appreciated.
I should add that the tag names (Country, EmployeeType and Language) are just examples, I can have the tag names and values (US, CA etc...) added run time.
I solved this. I had to change the schema and the rules of the group table a lil bit
Group Table
GroupId | Tags | OperatorGroup
---------------------------------------------------------------------------------------------
1 | Country|US | D68BF531-FD9B-4A0E-886E-7420BDFFE380
1 | EmployeeType|FullTime | D68BF531-FD9B-4A0E-886E-7420BDFFE380
1 | Country|CA | 0B97D792-7659-42EF-8036-D19B1A0D5197
1 | EmployeeType|FullTime | 0B97D792-7659-42EF-8036-D19B1A0D5197
2 | Country|BR | CE61BC17-2567-4B02-AE5E-F5EA40265EE7
2 | EmployeeType|FullTime | CE61BC17-2567-4B02-AE5E-F5EA40265EE7
3 | Country|US | BF3DAC05-8131-463F-976E-28099628842D
3 | EmployeeType|FullTime | BF3DAC05-8131-463F-976E-28099628842D
3 | Language|Spanish | BF3DAC05-8131-463F-976E-28099628842D
Each unique OperatorGroup within a value of GroupId represents a complete set of rules that match a specific group. That way I can pivot my employee and group tables and do a join to find matching groups.
I have pasted my schema data + queries below.
Schema
CREATE TABLE [dbo].[Employee]([EmployeeId] [int], [EmployerId] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL)
CREATE TABLE [dbo].[EmployeeTag]([EmployeeTagId] [int], [EmployeeId] [int] NOT NULL, [TagId] [int] NOT NULL)
CREATE TABLE [dbo].[Employer]([EmployerId] [int], [Name] [nvarchar](50) NOT NULL)
CREATE TABLE [dbo].[Group]([GroupId] [int], [EmployerId] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL)
CREATE TABLE [dbo].[GroupDefinition]([GroupDefinitionId] [int], [GroupId] [int] NOT NULL, [TagId] [int] NOT NULL, [Grouping] [uniqueidentifier] NOT NULL)
CREATE TABLE [dbo].[Tag]([TagId] [int], [Name] [nvarchar](50) NOT NULL, [Value] [nvarchar](50) NOT NULL)
Data
INSERT [dbo].[Employer] ([EmployerId], [Name]) VALUES (1, N'Fabrikam')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (1, 1, N'John Smith')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (2, 1, N'Jane Miller')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (3, 1, N'Nelda Maughan')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (4, 1, N'Joe Anderson')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (5, 1, N'Ami Mcmanis')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (6, 1, N'Charlena Ashbaugh')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (7, 1, N'Matt Tutino')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (8, 1, N'Sulema Alexandria')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (9, 1, N'Ema Wolak')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (10, 1, N'Marinda Krejci')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (11, 1, N'Felipa Chew')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (12, 1, N'Armand Hosier')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (13, 1, N'Roland Blunk')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (14, 1, N'Ellen Frasier')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (15, 1, N'Nicolle Tarbell')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (16, 1, N'Christopher Lautenschlage')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (17, 1, N'Nelson Maughan')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (18, 1, N'Wes Boyd')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (19, 1, N'Jason Lessley')
INSERT [dbo].[Employee] ([EmployeeId], [EmployerId], [Name]) VALUES (20, 1, N'Jesus Purves')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (1, N'Country', N'United States')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (2, N'Country', N'Canada')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (3, N'Country', N'Brazil')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (4, N'EmployeeType', N'Full Time')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (5, N'EmployeeType', N'Part Time')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (6, N'Language', N'Spanish')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (7, N'Language', N'French')
INSERT [dbo].[Tag] ([TagId], [Name], [Value]) VALUES (8, N'Language', N'English')
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (1, 1, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (2, 1, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (3, 1, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (4, 2, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (5, 2, 5)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (6, 2, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (7, 3, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (8, 3, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (9, 3, 7)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (10, 4, 3)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (11, 4, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (12, 4, 7)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (13, 5, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (14, 5, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (15, 5, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (16, 6, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (17, 6, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (18, 7, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (19, 7, 5)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (20, 7, 7)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (21, 8, 3)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (22, 8, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (23, 9, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (24, 9, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (25, 9, 7)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (26, 10, 3)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (27, 10, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (28, 11, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (29, 11, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (30, 11, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (31, 12, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (32, 12, 5)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (33, 12, 6)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (34, 13, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (35, 13, 5)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (36, 13, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (37, 14, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (38, 14, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (39, 14, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (40, 15, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (41, 15, 5)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (42, 15, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (43, 16, 3)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (44, 16, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (45, 17, 1)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (46, 17, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (47, 18, 8)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (48, 19, 2)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (49, 19, 7)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (50, 20, 3)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (51, 20, 4)
INSERT [dbo].[EmployeeTag] ([EmployeeTagId], [EmployeeId], [TagId]) VALUES (52, 20, 6)
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (1, 1, N'US Full Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (2, 1, N'CA Full Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (3, 1, N'NA Spanish Full Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (4, 1, N'NA French Full Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (5, 1, N'US Part Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (6, 1, N'CA Part Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (7, 1, N'NA Spanish Part Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (8, 1, N'NA French Part Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (9, 1, N'BR Spanish Full Timers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (10, 1, N'Spanish Speakers')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (11, 1, N'US Employees')
INSERT [dbo].[Group] ([GroupId], [EmployerId], [Name]) VALUES (12, 1, N'BR Employees')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (1, 1, 1, N'8e336244-94ac-4e62-a719-0a4a0b01a6f2')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (2, 1, 4, N'8e336244-94ac-4e62-a719-0a4a0b01a6f2')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (3, 2, 2, N'b3c87fe8-7b9f-4648-9708-20a460cf9314')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (4, 2, 4, N'b3c87fe8-7b9f-4648-9708-20a460cf9314')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (5, 3, 1, N'36fa7924-fa11-4772-b632-84c79b6bbb1b')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (6, 3, 4, N'36fa7924-fa11-4772-b632-84c79b6bbb1b')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (7, 3, 6, N'36fa7924-fa11-4772-b632-84c79b6bbb1b')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (8, 3, 2, N'dfa7f0f9-aa7a-401b-8bfd-804aeadff1a9')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (9, 3, 4, N'dfa7f0f9-aa7a-401b-8bfd-804aeadff1a9')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (10, 3, 6, N'dfa7f0f9-aa7a-401b-8bfd-804aeadff1a9')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (11, 4, 1, N'b857e999-0a7e-49fe-b138-88aed2d81aa3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (12, 4, 4, N'b857e999-0a7e-49fe-b138-88aed2d81aa3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (13, 4, 7, N'b857e999-0a7e-49fe-b138-88aed2d81aa3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (14, 4, 2, N'296741c2-de6f-46ca-adcc-5b552fb4b0eb')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (15, 4, 4, N'296741c2-de6f-46ca-adcc-5b552fb4b0eb')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (16, 4, 7, N'296741c2-de6f-46ca-adcc-5b552fb4b0eb')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (17, 5, 1, N'48fd52d4-141a-43d3-8dea-69b9df6337ed')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (18, 5, 5, N'48fd52d4-141a-43d3-8dea-69b9df6337ed')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (19, 6, 2, N'e85ba82a-0ca7-410b-ba35-726bd86a8621')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (20, 6, 5, N'e85ba82a-0ca7-410b-ba35-726bd86a8621')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (21, 7, 1, N'bb656fc1-8ccd-4ff6-85ec-185216b55e9f')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (22, 7, 5, N'bb656fc1-8ccd-4ff6-85ec-185216b55e9f')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (23, 7, 6, N'bb656fc1-8ccd-4ff6-85ec-185216b55e9f')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (24, 7, 2, N'8333dc24-4850-40d9-8b34-86c5d33bca60')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (25, 7, 5, N'8333dc24-4850-40d9-8b34-86c5d33bca60')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (26, 7, 6, N'8333dc24-4850-40d9-8b34-86c5d33bca60')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (27, 8, 1, N'e060b0e9-759f-4fe5-80d8-3fb2a73df6c8')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (28, 8, 5, N'e060b0e9-759f-4fe5-80d8-3fb2a73df6c8')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (29, 8, 7, N'e060b0e9-759f-4fe5-80d8-3fb2a73df6c8')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (30, 8, 2, N'33854572-3081-4e1c-ae98-fa15b2bc86b3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (31, 8, 5, N'33854572-3081-4e1c-ae98-fa15b2bc86b3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (32, 8, 7, N'33854572-3081-4e1c-ae98-fa15b2bc86b3')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (33, 9, 3, N'a4bfd6b8-556a-4e01-b3e8-b725a85c458c')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (34, 9, 4, N'a4bfd6b8-556a-4e01-b3e8-b725a85c458c')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (35, 9, 6, N'a4bfd6b8-556a-4e01-b3e8-b725a85c458c')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (36, 10, 6, N'649c0f7a-a5d3-4713-9335-8be57137347e')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (37, 11, 1, N'54c43caa-67a6-4be3-b237-2a89423360c8')
INSERT [dbo].[GroupDefinition] ([GroupDefinitionId], [GroupId], [TagId], [Grouping]) VALUES (38, 12, 3, N'd282d453-9671-408e-b6c6-d36296369a2e')
Query
DECLARE #employerId INT = 1
IF OBJECT_ID('tempdb..#pivotedEmployeeTags') IS NOT NULL DROP TABLE #pivotedEmployeeTags;
IF OBJECT_ID('tempdb..#pivotedGroupDefinition') IS NOT NULL DROP TABLE #pivotedGroupDefinition;
CREATE TABLE #pivotedEmployeeTags (EmployeeId INT, Name NVARCHAR(50), Country INT NULL, EmployeeType INT NULL, [Language] INT NULL)
CREATE TABLE #pivotedGroupDefinition (GroupId INT, Name NVARCHAR(50), Country INT NULL, EmployeeType INT NULL, [Language] INT NULL)
;WITH FlattenedEmployeeTags AS
(
SELECT
Employee.EmployeeId
,Employee.Name
,Tag.TagId
,Tag.Name as TagName
FROM Employee
LEFT JOIN EmployeeTag ON EmployeeTag.EmployeeId = Employee.EmployeeId
LEFT JOIN Tag ON Tag.TagId = EmployeeTag.TagId
WHERE EmployerId = #employerId
)
INSERT INTO #pivotedEmployeeTags
SELECT
EmployeeId
,Name
,Country = MAX( CASE WHEN TagName = 'Country' THEN TagId END )
,EmployeeType = MAX( CASE WHEN TagName = 'EmployeeType' THEN TagId END )
,[Language] = MAX( CASE WHEN TagName = 'Language' THEN TagId END )
FROM FlattenedEmployeeTags
GROUP BY EmployeeId, Name
;WITH FlattenedGroupDefinition AS
(
SELECT
[Group].GroupId
,[Group].Name
,Tag.TagId
,Tag.Name as TagName
,Grouping
FROM [Group]
LEFT JOIN GroupDefinition ON GroupDefinition.GroupId = [Group].GroupId
LEFT JOIN Tag ON Tag.TagId = GroupDefinition.TagId
WHERE EmployerId = #employerId
)
INSERT INTO #pivotedGroupDefinition
SELECT
GroupId
,Name
,Country = MAX( CASE WHEN TagName = 'Country' THEN TagId END )
,EmployeeType = MAX( CASE WHEN TagName = 'EmployeeType' THEN TagId END )
,[Language] = MAX( CASE WHEN TagName = 'Language' THEN TagId END )
FROM FlattenedGroupDefinition
GROUP BY GroupId, Name, Grouping
ORDER BY GroupId, Grouping
SELECT
EmpTags.EmployeeId
, GrpTags.GroupId
FROM
#pivotedEmployeeTags EmpTags
INNER JOIN #pivotedGroupDefinition GrpTags
ON
(GrpTags.Country = EmpTags.Country OR GrpTags.Country IS NULL)
AND (GrpTags.EmployeeType = EmpTags.EmployeeType OR GrpTags.EmployeeType IS NULL)
AND (GrpTags.[Language] = EmpTags.[Language] OR GrpTags.[Language] IS NULL)
ORDER BY EmpTags.EmployeeId, GrpTags.GroupId

SQL MIN() smaller/greater not working properly

I have this Data in DB
CREATE TABLE Stu_Table
(
Stu_Id VARCHAR(2),
Stu_Name VARCHAR(15),
Stu_Class VARCHAR(10),
sub_id VARCHAR(2),
marks VARCHAR(3)
);
INSERT INTO Stu_Table VALUES (1, 'Komal', 10, 1, 45);
INSERT INTO Stu_Table VALUES (2, 'Ajay', 10, 1, 56);
INSERT INTO Stu_Table VALUES (3, 'Rakesh', 10, 1, 67);
INSERT INTO Stu_Table VALUES (1, 'Komal', 10, 2, 47);
INSERT INTO Stu_Table VALUES (2, 'Ajay', 10, 2, 53);
INSERT INTO Stu_Table VALUES (3, 'Rakesh', 10, 2, 57);
INSERT INTO Stu_Table VALUES (1, 'Komal', 10, 3, 45);
INSERT INTO Stu_Table VALUES (2, 'Ajay', 10, 3, 56);
INSERT INTO Stu_Table VALUES (3, 'Rakesh', 10, 3, 67);
INSERT INTO Stu_Table VALUES (1, 'Komal', 10, 4, 65);
INSERT INTO Stu_Table VALUES (2, 'Ajay', 10, 4, 56);
INSERT INTO Stu_Table VALUES (3, 'Rakesh', 10, 4, 37);
INSERT INTO Stu_Table VALUES (1, 'Komal', 10, 5, 65);
INSERT INTO Stu_Table VALUES (2, 'Ajay', 10, 5, 46);
INSERT INTO Stu_Table VALUES (3, 'Rakesh', 10, 5, 63);
And I'm doing this query on this data.
SELECT *
FROM
(
SELECT
Stu_Id,
MIN(marks) AS mini,
AVG(marks) AS per
FROM stu_table
GROUP BY stu_id
HAVING MIN(marks) > 45
);
And I'm getting this:
Stu_Id| mini | per
1 | 45 | 53.4
2 | 46 | 53.4
3 | 37 | 58.2
I don't understand why I still see Stu_Id 1 with min(mark)=45 when I clearly have this HAVING min(marks)>45 in my query.
Runnable Demo
FIX:
Thanks to #sybkar and #Martin Smith!
I set the marks type as a string.
It's should be INT...
Thanks guys!
Working perfect!
create table Stu_Table(Stu_Id INT(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id INT(2),marks INT(3));<--INT!!!
I don't understand why I still see Stu_Id 1 with min(mark)=45
when I clearly have this HAVING min(marks)>45 in my query.
You don't. Or at the least the demo you have provided doesn't.
In general any weird results that you are getting will be because marks is being stored as a string so MIN(marks) will be bringing back the earliest in alphabetical order though.
For example HAVING MIN(marks) > 45 will also bring back 5, 6, 7, 8 and 9