Unknown column in where clause? - sql

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

Related

How to find if letter exist in string 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.

SQL exclude negative quantity

Please help, there is a request in which sometimes there is a negative value in the KOLVO_RZ column. Is it possible to check this in the request and if the value is negative, then return 0?
SELECT KOLVO_T - KOLVO_KKM - KOLVO_RZ AS KOLVO_T
FROM PRICE
This should do the trick:
SELECT KOLVO_T - KOLVO_KKM - (CASE WHEN KOLVO_RZ < 0 THEN 0 ELSE KOLVO_RZ END) AS KOLVO_T
FROM PRICE
Here's a sample that works for me with InterBase 2020. Which InterBase version are you using?
create table hikers (
hiker_id integer not null,
name varchar (256) not null,
constraint pk_hid primary key (hiker_id));
create table hiker_data (
hiker_id integer not null,
steps_gained integer,
constraint fk_hid foreign key (hiker_id) references hikers (hiker_id));
commit;
insert into hikers values (1, 'Aarti');
insert into hikers values (2, 'Bala');
insert into hikers values (3, 'Chaaya');
insert into hikers values (4, 'Deepak');
insert into hiker_data values (1, 20);
insert into hiker_data values (1, 10);
insert into hiker_data values (1, -5);
insert into hiker_data values (1, 15);
insert into hiker_data values (1, -20);
insert into hiker_data values (2, 12);
insert into hiker_data values (2, 6);
insert into hiker_data values (2, 20);
insert into hiker_data values (2, -5);
insert into hiker_data values (2, 10);
insert into hiker_data values (2, -18);
insert into hiker_data values (3, 20);
insert into hiker_data values (3, 10);
insert into hiker_data values (3, -5);
insert into hiker_data values (3, 15);
insert into hiker_data values (3, -20);
insert into hiker_data values (3, 12);
insert into hiker_data values (4, 6);
insert into hiker_data values (4, 20);
insert into hiker_data values (4, -5);
insert into hiker_data values (4, 10);
insert into hiker_data values (4, -18);
commit;
/* test foreign key enforcement */
insert into hiker_data values (-100, -18);
and then, I execute the following queries successfully.
select hiker_id,
SUM(0 + (CASE WHEN steps_gained < 0 THEN 0
ELSE steps_gained
END)) AS total_steps_climbed,
SUM(0 + (CASE WHEN steps_gained > 0 THEN 0
ELSE steps_gained
END)) AS total_steps_descended,
SUM(steps_gained) AS final_tally
from hiker_data
group by hiker_id;
select hiker_id,
(CASE WHEN steps_gained < 0 THEN 0
ELSE steps_gained
END) AS steps_climbed
from hiker_data
where hiker_id=2;

SQL script for multiple reservations by one customer (Restaurant)

I need my SQL db to be able to hold more than one reservation for a customer.
However I get some duplicate pk errors.
Here is my SQL script: (I wanna be able to let one customer have more than 1 reserv.)
/*
db_name: Restaurant DB
update_timestamp: 6.10.2021 # 09:48
sign: The Actual Daniel
*/
DROP DATABASE IF EXISTS restaurantDB;
CREATE DATABASE restaurantDB;
USE restaurantDB;
CREATE TABLE `bill` (
`billID` int NOT NULL AUTO_INCREMENT,
`payAmount` varchar(255) NOT NULL,
PRIMARY KEY (`billID`)
);
INSERT INTO `bill` VALUES (1, '$258');
INSERT INTO `bill` VALUES (2, '$420');
INSERT INTO `bill` VALUES (3, '$114');
INSERT INTO `bill` VALUES (4, '$88');
INSERT INTO `bill` VALUES (5, '$137');
INSERT INTO `bill` VALUES (6, '$286');
INSERT INTO `bill` VALUES (7, '$97');
INSERT INTO `bill` VALUES (8, '$110');
INSERT INTO `bill` VALUES (9, '$358');
INSERT INTO `bill` VALUES (10, '$190');
CREATE TABLE `customer` (
`customerID` int NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`customerID`)
);
INSERT INTO `customer` VALUES (1, 'Nero', 'Lee', '420420589');
INSERT INTO `customer` VALUES (2, 'Lucius', 'Malt', '578632221');
INSERT INTO `customer` VALUES (3, 'Aurelian', 'Keen', '639456773');
INSERT INTO `customer` VALUES (4, 'Jasmine', 'Wang', '227453891');
INSERT INTO `customer` VALUES (5, 'Sophie', 'Clinck', '905674321');
INSERT INTO `customer` VALUES (6, 'Augustus', 'Smith', '523764112');
INSERT INTO `customer` VALUES (7, 'Isabelle', 'Niaom', '786554330');
INSERT INTO `customer` VALUES (8, 'Alix', 'Black', '009886775');
INSERT INTO `customer` VALUES (9, 'Leia', 'Mudworth', '345667228');
INSERT INTO `customer` VALUES (10, 'Claudius', 'Reed', '427654221');
CREATE TABLE `meal` (
`mealID` int NOT NULL AUTO_INCREMENT,
`sides` varchar(255) NULL,
`main` varchar(255) NULL,
`beverage` varchar(255) NOT NULL,
PRIMARY KEY (`mealID`)
);
INSERT INTO `meal` VALUES (1, 'potatos', 'steak', 'wine');
INSERT INTO `meal` VALUES (2, 'ice', 'cube steak', 'spring water');
INSERT INTO `meal` VALUES (3, 'rice', 'cake', 'beer');
INSERT INTO `meal` VALUES (4, 'salad', 'pasta', 'wine');
INSERT INTO `meal` VALUES (5, 'radish', 'noodles', 'soju');
INSERT INTO `meal` VALUES (6, 'rice', 'rice', 'pumpkin juice');
INSERT INTO `meal` VALUES (7, 'butter, honey', 'steak', 'wine');
INSERT INTO `meal` VALUES (8, 'ice', 'salad', 'mineral water');
INSERT INTO `meal` VALUES (9, 'sweet potatos', 'pork', 'beer');
INSERT INTO `meal` VALUES (10, 'sourdough bread', 'fruit bowl', 'mineral water');
CREATE TABLE `order` (
`orderID` int NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`orderID`)
);
INSERT INTO `order` VALUES (1, '21-09-27 13:36:06');
INSERT INTO `order` VALUES (2, '21-02-14 17:18:03');
INSERT INTO `order` VALUES (3, '21-03-27 11:34:16');
INSERT INTO `order` VALUES (4, '21-07-14 15:58:32');
INSERT INTO `order` VALUES (5, '21-02-27 18:16:33');
INSERT INTO `order` VALUES (6, '21-01-14 19:28:12');
INSERT INTO `order` VALUES (7, '21-08-27 16:27:07');
INSERT INTO `order` VALUES (8, '21-06-14 11:21:09');
INSERT INTO `order` VALUES (9, '21-11-27 16:30:03');
INSERT INTO `order` VALUES (10, '21-03-14 17:22:11');
CREATE TABLE `reservation` (
`reservationID` int NOT NULL AUTO_INCREMENT,
`checkIn` datetime NOT NULL,
`checkOut` datetime NOT NULL,
`customerForeign` int NOT NULL,
PRIMARY KEY (`reservationID`),
FOREIGN KEY (`customerForeign`) REFERENCES customer(customerID)
);
INSERT INTO `reservation` VALUES (1, '21-09-27 13:24:06', '21-09-27 14:17:23', 1);
INSERT INTO `reservation` VALUES (2, '21-02-14 17:25:03', '21-02-14 19:22:14', 2);
INSERT INTO `reservation` VALUES (3, '21-03-27 11:34:16', '21-03-27 13:34:16', 3);
INSERT INTO `reservation` VALUES (4, '21-07-14 15:58:32', '21-07-14 16:58:32', 4);
INSERT INTO `reservation` VALUES (5, '21-02-27 18:16:33', '21-02-27 19:16:33', 5);
INSERT INTO `reservation` VALUES (6, '21-01-14 19:28:12', '21-01-14 20:28:12', 6);
INSERT INTO `reservation` VALUES (7, '21-08-27 16:27:07', '21-08-27 17:27:07', 7);
INSERT INTO `reservation` VALUES (8, '21-06-14 11:21:09', '21-06-14 14:21:09', 8);
INSERT INTO `reservation` VALUES (9, '21-11-27 16:30:03', '21-11-27 17:30:03', 9);
INSERT INTO `reservation` VALUES (10, '21-03-14 17:22:11', '21-03-14 19:22:11', 10);
CREATE TABLE `table` (
`tableID` int NOT NULL,
`numOfSeats` int NOT NULL,
PRIMARY KEY (`tableID`)
);
INSERT INTO `table` VALUES (1, 1);
INSERT INTO `table` VALUES (2, 2);
INSERT INTO `table` VALUES (3, 2);
INSERT INTO `table` VALUES (4, 2);
INSERT INTO `table` VALUES (5, 4);
INSERT INTO `table` VALUES (6, 4);
INSERT INTO `table` VALUES (7, 6);
INSERT INTO `table` VALUES (8, 6);
INSERT INTO `table` VALUES (9, 8);
INSERT INTO `table` VALUES (10, 12);
ALTER TABLE `bill` ADD CONSTRAINT `fk_bill_order_1` FOREIGN KEY (`billID`) REFERENCES `order` (`orderID`);
ALTER TABLE `meal` ADD CONSTRAINT `fk_meal_order_1` FOREIGN KEY (`mealID`) REFERENCES `order` (`orderID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_customer_1` FOREIGN KEY (`orderID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `reservation` ADD CONSTRAINT `fk_reservation_customer_1` FOREIGN KEY (`reservationID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `table` ADD CONSTRAINT `fk_table_reservation_1` FOREIGN KEY (`tableID`) REFERENCES `reservation` (`reservationID`);
This is error I get when trying to insert new reservation for customer with ID of 1

sql join joining 3 tables

bit of a strange one this one...
Someone wrote a sql that in my mind shouldn't work, but it does and it also returns the correct results. I've written a simplified example, but I think it shows the point.
drop table #client;
drop table #transactions;
drop table #history;
create table #client (
clientId int,
name varchar(50)
);
create table #transactions (
transid int,
clientId int,
Amount int
);
create table #history (
transid int,
Amount int
);
insert into #client values (1, 'User 1');
insert into #client values (2, 'User 2');
insert into #client values (3, 'User 3');
insert into #transactions values (1, 1, 50);
insert into #transactions values (2, 1, 35);
insert into #transactions values (3, 1, 25);
insert into #transactions values (4, 2, 10);
insert into #transactions values (5, 2, 50);
insert into #transactions values (6, 1, 35);
insert into #transactions values (7, 3, 25);
insert into #transactions values (8, 3, 10);
insert into #history values (1, 50);
insert into #history values (2, 35);
insert into #history values (3, 25);
insert into #history values (4, 10);
insert into #history values (5, 50);
insert into #history values (6, 35);
insert into #history values (7, 25);
insert into #history values (8, 10);
select * from #history
join #transactions on #history.transid = #transactions.transid
join #client on #transactions.clientId = #client.clientId and #history.transid = #transactions.transid
The last join joins 3 tables together in one join, which shouldn't work from what I know of writing SQL. Am I wrong (which may very well be the case)
Why would you think it shouldn't work?
The last bit of the query is pointless though, and you can take it out. It's a simple join on 3 tables:
select * from #history
join #transactions on #history.transid = #transactions.transid
join #client on #transactions.clientId = #client.clientId

SQL Query count

HI there I have this table,
Recipe = (idR, recipeTitle, prepText, cuisineType, mealType)
Ingredient = (idI, ingrDesc)
RecipIngr = (idR*, idI*)
and I'm trying to query a list for ingrDesc with a count of how many recipies that ingrDesc is in. I want to list only those ingrDesc that occur more than 10 times.
Here's what I have:
SELECT a.idI, a.recipeTitle
FROM Recipe a
INNER JOIN recpingr b
ON a.idr = b.idr
WHERE a.preptext = '>10'
Any help as I don't know how to carry on with this query
Use GROUP BY with HAVING:
SELECT i.idI, i.ingrDesc, COUNT(*)
FROM Ingredient i
INNER JOIN RecipIngr ri ON i.idI = ri.idI
GROUP BY i.idI, i.ingrDesc
HAVING COUNT(*) > 10
You need to use a group by clause and having. I have created a quick sample here but my sample data does not go up to 10 so I used any ingredient that was used more than once (> 1).
Here is the sample data:
create table dbo.recipe (
idR int not null,
recipeTitle varchar(100) not null,
prepText varchar(4000) null,
cuisineType varchar(100) null,
mealType varchar(100) null
)
go
insert into dbo.recipe values (1, 'Eggs and Bacon', 'Prep Text 1', 'American', 'Breakfast')
insert into dbo.recipe values (2, 'Turkey Sandwich', 'Prep Text 2', 'American', 'Lunch')
insert into dbo.recipe values (3, 'Roast Beef Sandwich', 'Prep Text 3', 'American', 'Lunch')
go
create table dbo.ingredient (
idI int not null,
ingrDesc varchar(200) not null
)
go
insert into dbo.ingredient values (1, 'Large Egg')
insert into dbo.ingredient values (2, 'Bacon');
insert into dbo.ingredient values (3, 'Butter');
insert into dbo.ingredient values (4, 'Sliced Turkey');
insert into dbo.ingredient values (5, 'Lettuce');
insert into dbo.ingredient values (6, 'Tomato');
insert into dbo.ingredient values (7, 'Onion');
insert into dbo.ingredient values (8, 'Bread');
insert into dbo.ingredient values (9, 'Mustard');
insert into dbo.ingredient values (10, 'Horseradish');
insert into dbo.ingredient values (11, 'Sliced Roast Beef');
go
create table dbo.recipingr(
idR int not null,
idI int not null
)
go
insert into dbo.recipingr values (1, 1);
insert into dbo.recipingr values (1, 2);
insert into dbo.recipingr values (2, 4);
insert into dbo.recipingr values (2, 5);
insert into dbo.recipingr values (2, 6);
insert into dbo.recipingr values (2, 7);
insert into dbo.recipingr values (2, 8);
insert into dbo.recipingr values (2, 9);
insert into dbo.recipingr values (3, 11);
insert into dbo.recipingr values (3, 10);
insert into dbo.recipingr values (3, 8);
insert into dbo.recipingr values (3, 6);
insert into dbo.recipingr values (3, 5);
go
Here is the query:
select
i.ingrDesc,
count(*) ingrCount
from
dbo.recipe r
inner join dbo.recipingr ri on ri.idR = r.idR
inner join dbo.ingredient i on i.idI = ri.idI
group by
i.ingrDesc
having
count(*) > 1