SELECT tecajnica.simbol, vp.opis, tecajnica.eTecaj
FROM tecajnica LEFT JOIN vp
ON tecajnica.simbol = vp.simbol
WHERE tecajnica.simbol LIKE 'Z%' AND
tecajnica.datum = '2004-02-26';
SELECT
SELECT
torej vrne tabelo!SELECT
- osnovna strukturaSELECT
seznam stolpcev, ki jih želimo videti
FROM
seznam tabel, kjer bomo podatke našli
WHERE
pogoji, ki določajo, katere podatke želimo videti
SELECT * FROM knjige
WHERE cena > 100.00
ORDER BY naslov;
SELECT * FROM tabela
*
tabela
SELECT * FROM drzave;
SELECT s1, s2 FROM tabPod
s1, s2
s1
in s2
iz tabele tabPod
SELECT name, population FROM world;
SELECT population, name FROM world;
SELECT s1, s2 FROM tabPod
(2)AS
: poimenovanje stolpca v izhodu
SELECT name AS drzava, population AS st_prebivalcev FROM world;
SELECT name AS "Ime države" FROM world;
SELECT DISTINCT
SELECT DISTINCT continent FROM world;
SELECT leto FROM film;
SELECT DISTINCT leto FROM film;
Ime |
Priimek |
Starost |
Kraj |
---|---|---|---|
Janez | Slovenec | 42 | Kranj |
Ivan | Slovenec | 44 | Celje |
Peter | Slovenec | 33 | Kranj |
Miha | Slovenski | 12 | Celje |
</small>
SELECT DISTINCT Kraj FROM t; -- 2 vrstici
SELECT DISTINCT Priimek, Kraj FROM t; -- 3 vrstice
SELECT DISTINCT Ime, Priimek, Kraj FROM t; -- 4 vrstice
SELECT Kraj FROM t; -- 4 vrstice
SELECT Priimek, Kraj FROM t; -- 4 vrstice
SELECT Ime, Priimek, Kraj FROM t; -- 4 vrstice
SELECT * FROM world WHERE continent = 'Europe';
=
, <>
, <
, <=
, >
, >=
,IS NULL
, IS NOT NULL
,e [NOT] BETWEEN a AND b
,e [NOT] IN (v1, v2, ...)
,e [NOT] LIKE vzorec
,NOT
, AND
, OR
, XOR
(in še kakimi specifičnimi za vsak RDBMS).SELECT name, population / area
FROM world
WHERE continent = 'Europe';
SELECT name AS ime_drzave,
population / area AS gostota_prebivalstva
FROM world
WHERE continent = 'Europe';
SELECT name AS "ime države",
ROUND(population / area, 2) AS "gostota prebivalstva"
FROM world
WHERE continent = 'Europe' AND population > 2000000;
ORDER BY
.SELECT name, population FROM world ORDER BY population;
SELECT name, population FROM world ORDER BY 2;
-- Uredimo padajoče
SELECT name, population FROM world ORDER BY population DESC;
SELECT name FROM world
WHERE continent = 'Europe'
ORDER BY population/area;
SELECT
- dodatkiSELECT naslov, ROUND(cena * 1.10, 2) FROM knjige
WHERE cena * (1 + davek/100.0) > 100.00;
AS
: pre(po)imenujemo izhodni stolpecORDER BY
: uredimo vrsticeDISTINCT
: v končnem rezultatu upoštevamo le različne vrsticeSELECT
- primerSELECT name, ROUND(population/1000000) AS prebMilijoni
FROM world
WHERE continent IN ('Asia', 'Europe') AND
name LIKE 'C%';
prebMilijoni
.world
.continent
bodisi Asia bodisi Europe in kjer se vrednost v stolpcu ime
začne s črko C.SELECT
SELECT * FROM world;
SELECT name, population FROM world;
SELECT name AS "Ime države" FROM world;
SELECT name AS Ime FROM world
ORDER BY population DESC;
SELECT DISTINCT continent FROM world;
SELECT 2 FROM world WHERE continent = 'Europe';
SELECT population/area FROM world
WHERE continent IN ('Europe', 'Asia', 'Africa') AND
area BETWEEN 100000 AND 1000000;
SELECT name FROM world
WHERE continent = 'Europe'
ORDER BY population/area;
SELECT population FROM world WHERE name = 'Slovenia';
SELECT name FROM world WHERE population <= 2063151;
SELECT
SELECT name FROM world
WHERE population <= (
SELECT population FROM world
WHERE name = 'Slovenia'
);
population
, kjer upoštevamo le tiste vrstice, kjer je ime države Slovenia.SELECT name FROM world
WHERE population < (
SELECT population FROM world
WHERE name = 'Slovenia'
) AND
continent = 'Europe';
SELECT population FROM world
WHERE name = 'Canada';
SELECT population FROM world
WHERE name = 'Algeria';
SELECT name FROM world
WHERE population BETWEEN 35427524 AND 38700000;
SELECT name FROM world
WHERE population BETWEEN (
SELECT population FROM world
WHERE name = 'Canada'
) AND (
SELECT population FROM world
WHERE name = 'Algeria'
);
e [NOT] IN (SELECT ... )
- ali je (ni) vrednost e
ena izmed vrednosti v stolpcue < ALL (SELECT ...)
- ali je vrednost e
manjša od vseh vrednosti v stolpcue >= ANY (SELECT ...)
- ali je vrednost e
večja ali enaka vsaj eni vrednosti v stolpcu[NOT] EXISTS (SELECT ...)
- ali podpoizvedba vrne vsaj eno vrstico (oz. nobene)nobel(yr, subject, winner)
subject = 'physics'
.yr
ena od tistih vrednosti, ki nastopajo v stolpcu yr
pri tistih vrsticah, kjer je subject = 'chemistry'
.SELECT DISTINCT yr FROM nobel
WHERE subject = 'physics' AND
yr NOT IN (
SELECT yr FROM nobel
WHERE subject = 'chemistry'
);
DISTINCT
?
WITH
SELECT naslov, ocena FROM film
WHERE ocena IN (
SELECT ocena FROM film
WHERE naslov LIKE 'Scary Movie%' OR
naslov = 'Film da te kap'
);
WITH
si lahko pripravimo podpoizvedbo in se nanjo sklicujemo z določenim imenom.
WITH ocene_strasnih_filmov AS (
SELECT ocena FROM film
WHERE naslov LIKE 'Scary Movie%' OR
naslov = 'Film da te kap'
)
SELECT naslov, ocena FROM film
WHERE ocena IN ocene_strasnih_filmov;
WITH
(2)WITH gospodarji_prstanov AS (
SELECT * FROM film
WHERE naslov LIKE 'Gospodar prstanov%'
),
leta_gospodarjev AS (
SELECT leto FROM gospodarji_prstanov
),
ocene_gospodarjev AS (
SELECT ocena FROM gospodarji_prstanov
)
SELECT * FROM film
WHERE leto IN leta_gospodarjev AND
ocena IN ocene_gospodarjev;
SUM(stolpec)
: vsota vrednosti v stolpcu
SELECT SUM(gdp) FROM world
WHERE continent = 'Europe';
AVG(stolpec)
: povprečna vrednost v stolpcu
SELECT AVG(population) FROM world
WHERE continent = 'Europe';
MIN(stolpec)
: minimalna vrednost v stolpcu
SELECT MIN(area) FROM world;
MAX(stolpec)
: maksimalna vrednost v stolpcu
SELECT MAX(population) FROM world
WHERE continent = 'Africa';
SUM
, AVG
, MIN
, MAX
upoštevajo le vrednosti, ki niso NULL
!COUNT(stolpec)
: število vrstic z neničelno vrednostjo v stolpcu
SELECT COUNT(oznaka) FROM film;
COUNT(DISTINCT stolpec)
: število različnih vrednosti v stolpcu
SELECT COUNT(DISTINCT oznaka) FROM film;
COUNT(*)
: število vrstic (zapisov)
SELECT COUNT(*) FROM world
WHERE continent LIKE '%America%';
SELECT * FROM world
WHERE name LIKE 'S%' AND
continent = 'Africa' AND
name NOT LIKE '% %' AND
name <> 'Senegal';
SELECT AVG(gdp) FROM world
WHERE name LIKE 'S%' AND
continent = 'Africa' AND
name NOT LIKE '% %' AND
name <> 'Senegal';
SELECT naslov, MIN(LENGTH(naslov)) FROM film;
SELECT naslov, LENGTH(naslov) AS dolzina FROM film
ORDER BY dolzina;
SELECT name, MAX(population) FROM world
WHERE continent = 'Africa';
Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no
GROUP columns is illegal if there is no GROUP BY clause
SELECT name, population FROM world
WHERE continent = 'Africa' AND
population = MAX(population);
WHERE
, saj tukaj preverjamo pogoje na eni vrstici naenkrat!SELECT name, population FROM world
WHERE continent = 'Africa' AND
population = (
SELECT MAX(population) FROM world
WHERE continent = 'Africa'
);
SELECT name FROM world
WHERE gdp > (
SELECT MAX(gdp) FROM world
WHERE continent = 'Europe'
);
ALL
, mora biti pogoj izpolnjen za vse rezultate podpoizvedbe.
SELECT name FROM world
WHERE gdp > ALL (
SELECT gdp FROM world
WHERE continent = 'Europe'
);
NULL
?NULL
NULL
vračajo NULL
!IS [NOT] NULL
:
SELECT name FROM world
WHERE gdp > ALL (
SELECT gdp FROM world
WHERE continent = 'Europe' AND
gdp IS NOT NULL
);
SELECT
bo enostaven:
SELECT name, continent, population FROM world
WHERE continent IN (
-- seznam ustreznih celin
);
MAX
za Evropo.
SELECT continent FROM world
WHERE 30000000 > (
SELECT MAX(population) FROM world
WHERE continent = 'Europe'
);
SELECT continent FROM world
WHERE 30000000 > (
SELECT MAX(population) FROM world
WHERE continent = 'Oceania'
);
COUNT
, vidimo, da dobimo VSE države (195).SELECT continent, name, population FROM world AS t1
WHERE population >= ALL (
SELECT population FROM world AS t2
WHERE t1.continent = t2.continent
);
world AS t1
?
world
smo preimenovali v t1
.t1
za sklicevanje na tabelo world
iz zunanje poizvedbe.t1.continent
: stolpec continent
iz tabele t1
.world
:
t1
v zunanji poizvedbi, int2
v podpoizvedbi.world
:
t1
se nanaša na trenutno vrstico v zunanji zanki,t2
se nanaša na trenutno vrstico v notranji zanki.SELECT continent FROM world AS t1
WHERE 30000000 > ALL (
SELECT population FROM world AS t2
WHERE t1.continent = t2.continent
);
DISTINCT
, saj nas zanima samo seznam celin.SELECT name, continent, population FROM world
WHERE continent IN (
SELECT continent FROM world AS t1
WHERE 30000000 > ALL (
SELECT population FROM world AS t2
WHERE t1.continent = t2.continent
)
);
SELECT name, continent, population FROM world AS t1
WHERE 30000000 > ALL (
SELECT population FROM world AS t2
WHERE t1.continent = t2.continent
);
SELECT name, continent FROM world AS t1
WHERE population > (
SELECT 3 * MAX(population) FROM world AS t2
WHERE t1.continent = t2.continent AND
t1.name <> t2.name
);
SELECT continent, population FROM world AS t1
WHERE population >= ALL (
SELECT population FROM world AS t2
WHERE t1.continent = t2.continent
);
GROUP BY
SELECT ... FROM tabela
...
GROUP BY stolpci;
GROUP BY
- primer
SELECT ... FROM donacije;
Ime | Prispevek |
---|---|
Anja | 25 |
Janez | 62 |
Anja | 60 |
Špela | 390 |
Špela | 25 |
</span>
SELECT ... FROM donacije
GROUP BY ime;
Ime | Prispevek |
---|---|
Anja | 25 |
Anja | 60 |
Janez | 62 |
Špela | 390 |
Špela | 25 |
</span>
SELECT ... FROM donacije
GROUP BY prispevek/100;
Ime | Prispevek |
---|---|
Anja | 25 |
Anja | 60 |
Janez | 62 |
Špela | 25 |
Špela | 390 |
</span> </span>
GROUP BY
:
SELECT MAX(population) FROM world
GROUP BY continent;
SELECT continent, MAX(population) FROM world
GROUP BY continent;
SELECT ime, prispevek FROM donacije GROUP BY ime;
SELECT ime, prispevek FROM donacije GROUP BY prispevek / 100;
GROUP BY
, bo v izhodu ena vrstica za vsako skupino.GROUP BY
, izven združevalnih funkcij.SELECT name, continent, MAX(population) FROM world
GROUP BY continent;
Expression #1 of SELECT list is not in GROUP BY clause
and contains nonaggregated column 'gisq.world.name'
which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
GROUP BY
in WHERE
Takih držav je 7:
SELECT name, population FROM world
WHERE population >= 160000000;
</span>
Dobimo:
name |
population |
---|---|
Brazil | 202794000 |
China | 1365370000 |
India | 1246160000 |
Indonesia | 252164800 |
Nigeria | 178517000 |
Pakistan | 188020000 |
United States | 318320000 |
</span> </span>
GROUP BY
in WHERE
(2)SELECT continent, COUNT(*) AS stevilo FROM world
WHERE population >= 160000000
GROUP BY continent;
continent |
stevilo |
---|---|
Africa | 1 |
Asia | 4 |
North America | 1 |
South America | 1 |
</span>
WHERE
deluje pred združevanjem.GROUP BY
razdelili v skupine po celinah.COUNT
pa je potem preštel število vrstic v posamezni skupini.</span> </span>
GROUP BY
:
SELECT DISTINCT continent FROM world AS t1
WHERE 500000000 <= (
SELECT SUM(population) FROM world AS t2
WHERE t1.continent = t2.continent
);
GROUP BY
.SELECT continent, SUM(population) FROM world
GROUP BY continent;
WHERE
?
GROUP BY
in HAVING
HAVING
filtriramo skupine, nastale pri združevanju.
SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) >= 500000000;
SUM(population)
potem seštejo prebivalstvo na posamezni celini.HAVING
preverja vrstice, ki jo dobimo za vsako skupino.HAVING
smemo uporabiti tiste stolpce/izraze, ki jih lahko ob uporabi GROUP BY
izberemo za stolpce dobljene tabele.nobel(yr, subject, winner)
SELECT yr FROM nobel
WHERE subject = 'Physics' AND
yr > 1970
GROUP BY yr
HAVING COUNT(yr) = 1;
SELECT winner, COUNT(subject) FROM nobel
GROUP BY winner
HAVING COUNT(subject) > 1;
winner |
COUNT(subject) |
---|---|
Frederick Sanger | 2 |
John Bardeen | 2 |
International Committee of the Red Cross | 3 |
Linus Pauling | 2 |
Marie Curie | 2 |
SELECT * FROM nobel
WHERE winner IN (
SELECT winner FROM nobel
GROUP BY winner
HAVING COUNT(subject) > 1
);
SELECT winner, COUNT(DISTINCT subject) AS podrocja FROM nobel
GROUP BY winner
HAVING COUNT(DISTINCT subject) > 1;
winner |
podrocja |
---|---|
Linus Pauling | 2 |
Marie Curie | 2 |
</span>
yr |
subject |
winner |
---|---|---|
1962 | Peace | Linus Pauling |
1954 | Chemistry | Linus Pauling |
1911 | Chemistry | Marie Curie |
1903 | Physics | Marie Curie |
</span> </span>
Pri GROUP BY
lahko uporabimo tudi več stolpcev (ključev).
Ime | Starost | Kraj |
---|---|---|
Janez | 18 | Kranj |
Metka | 18 | Kranj |
Peter | 20 | Kranj |
Pavel | 21 | Ljubljana |
Ivana | 18 | Ljubljana |
Silva | 19 | Ljubljana |
Srečko | 20 | Celje |
SELECT ... FROM t GROUP BY starost; -- 4 skupine
SELECT ... FROM t GROUP BY kraj; -- 3 skupine
SELECT ... FROM t GROUP BY starost, kraj; -- 6 skupin
</span>
GROUP BY yr, subject
GROUP BY subject, yr
WHERE yr > 2000
bomo omejili leta.HAVING COUNT(*) >= 3
bomo upoštevali le skupine z vsaj tremi vrsticami.
SELECT yr, subject FROM nobel
WHERE yr > 2000
GROUP BY yr, subject
HAVING COUNT(*) >= 3
ORDER BY yr;
Denimo, da imamo sledeči tabeli o olimpijskih igrah.
Leto | Kraj |
---|---|
1896 | Atene |
1948 | London |
2004 | Atene |
2008 | Beijing |
2012 | London |
Kraj | Država |
---|---|
Atene | Grčija |
London | Velika Britanija |
Beijing | Kitajska |
</span>
</span>
Da odgovorimo, potrebujemo podatke obeh tabel.
Leto | Kraj | Država |
---|---|---|
1896 | Atene | Grčija |
1948 | London | Velika Britanija |
2004 | Atene | Grčija |
2008 | Beijing | Kitajska |
2012 | London | Velika Britanija |
</span> </span>
Leto | Kraj |
---|---|
1896 | Atene |
1948 | London |
2004 | Atene |
2008 | Beijing |
2012 | London |
Kraj | Država |
---|---|
Atene | Grčija |
London | Velika Britanija |
Sydney | Avstralija |
</span>
</span>
Leto | Kraj | Država |
---|---|---|
1896 | Atene | Grčija |
1948 | London | Velika Britanija |
2004 | Atene | Grčija |
2008 | Beijing | |
2012 | London | Velika Britanija |
Sydney | Avstralija |
</span> </span>
SELECT
in več tabelPogosto želimo kot rezultat dobiti podatke, ki se nahajajo v več tabelah.
ID |
ImePriimek |
Posta |
---|---|---|
MK1 | Miha Kranjc | 1000 |
MH1 | Maja Hrust | 2000 |
LS1 | Lidija Svet | 1000 |
MK2 | Mitja Kern | 3000 |
Posta |
Kraj |
---|---|
1000 | Ljubljana |
2000 | Maribor |
3000 | Celje |
</span>
ID |
ImePriimek |
Posta |
Kraj |
---|---|---|---|
MK1 | Miha Kranjc | 1000 | Ljubljana |
MH1 | Maja Hrust | 2000 | Maribor |
LS1 | Lidija Svet | 1000 | Ljubljana |
MK2 | Mitja Kern | 3000 | Celje |
</span> </span>
SELECT * FROM zaposleni, posta;
SELECT ImePriimek, Posta, Kraj FROM zaposleni, posta;
Posta
?
SELECT ImePriimek, posta.Posta, Kraj FROM zaposleni, posta;
tabela.stolpec
.SELECT * FROM zaposleni, posta
WHERE zaposleni.Posta = posta.Posta;
Ker smo uporabili *
, dobimo vse stolpce iz obeh tabel.
ID |
ImePriimek |
Posta |
Posta |
Kraj |
---|---|---|---|---|
MK1 | Miha Kranjc | 1000 | 1000 | Ljubljana |
MH1 | Maja Hrust | 2000 | 2000 | Maribor |
LS1 | Lidija Svet | 1000 | 1000 | Ljubljana |
MK2 | Mitja Kern | 3000 | 3000 | Celje |
Posta
dobimo iz obeh tabel!SELECT * FROM zaposleni, posta
WHERE zaposleni.Posta = posta.Posta AND
ID = 'MK1';
WHERE
:
zaposleni.Posta = posta.Posta
)ID = 'MK1'
)INNER JOIN
)SELECT * FROM zaposleni
INNER JOIN posta
ON zaposleni.Posta = posta.Posta
WHERE ID = 'MK1';
INNER
lahko izpustimo.posta
), teh vrstic ne bo v izhodu!OUTER JOIN
)SELECT * FROM oi
LEFT OUTER JOIN kraj
ON oi.kraj = kraj.kraj;
LEFT [OUTER] JOIN
- ohrani vse vrstice iz prve tabeleRIGHT [OUTER] JOIN
- ohrani vse vrstice iz druge tabeleFULL [OUTER] JOIN
- ohrani vse vrstice iz obeh tabelON
ni nikoli izpolnjen, se ohranijo, vrednosti v stolpcih iz druge tabele pa dobijo vrednosti NULL
.WHERE
se zgodi po stikanju (in lahko izloči prej ohranjene vrstice).ON
je lahko poljuben (ne nujno ujemanje po stolpcih)!ON
naštejemo stolpce z določilom USING
:
SELECT * FROM zaposleni
INNER JOIN posta USING (Posta);
NATURAL JOIN
:
SELECT * FROM oi
NATURAL LEFT OUTER JOIN kraj;
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
movie
.actor
.casting
povezuje igralce in filme.
movie
in actor
(igralec je igral v več filmih, filmi pa imajo več igralcev)SELECT title FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actorid = actor.id
WHERE actor.name = 'John Wayne';
WHERE
.
movie
.casting
, ki imajo vrednost v stolpcu movieid
enako vrednosti stolpca id
tabele movie
.actor
, ki imajo vrednost v stolpcu id
enako vrednosti stolpca actorid
tabele casting
.name
v tabeli actor
enaka John Wayne.casting
.
movie
.actor
.casting.ord
).
SELECT name FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE title = 'Giant' AND
name <> 'James Dean'
ORDER BY ord;
casting
se pojavijo vsaj desetkrat z ord = 1
.
WHERE ord = 1
HAVING COUNT(*) >= 10
actor
.SELECT name FROM actor
JOIN casting ON actorid = id
WHERE ord = 1
GROUP BY id, name
HAVING COUNT(*) >= 10;
WHERE ord = 1
AND name <> 'Al Pacino'
AND movie.id IN (
-- seznam ID-jev filmov, kjer je igral Al Pacino
)
casting
in actor
.casting
!SELECT movieid FROM casting
JOIN actor ON actor.id = actorid
WHERE actor.name = 'Al Pacino';
SELECT title, name FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actor.id = actorid
WHERE ord = 1 AND
name <> 'Al Pacino' AND
movie.id IN (
SELECT movieid FROM casting
JOIN actor ON actor.id = actorid
WHERE actor.name = 'Al Pacino'
);
SELECT [DISTINCT] -- 8
stolpci, združevalne_funkcije -- 7
FROM leva_tabela -- 1
[LEFT] JOIN -- 3
desna_tabela ON pogoji_stikanja -- 2
WHERE vsebinski_pogoji -- 4
GROUP BY ključi_skupin -- 5
HAVING pogoji_skupin -- 6
ORDER BY ključi_urejanja -- 9
LIMIT število; -- 10
Imamo tabeli narocnik
in narocilo
.
narocnik_id |
ime |
priimek |
---|---|---|
1 | Damjan | Kmetec |
2 | Aljaž | Kmetec |
3 | Špela | Prezelj |
4 | Metka | Kmetec |
</span>
narocilo_id |
narocnik_id |
kolicina |
---|---|---|
1 | 1 | 2 |
2 | 1 | 1 |
3 | 3 | 1 |
4 | 4 | 4 |
</span> </span>
Želimo dobiti število naročil vsakega naročnika s priimkom Kmetec, ki je naročil največ tri kose, urejeno naraščajoče po številu naročil.
narocnik_id |
ime |
priimek |
stevilo |
skupna_kolicina |
---|---|---|---|---|
2 | Aljaž | Kmetec | 0 | 0 |
1 | Damjan | Kmetec | 2 | 3 |
</span>
SELECT narocnik_id, ime, priimek,
COUNT(narocilo_id) AS stevilo,
COALESCE(SUM(kolicina), 0) AS skupna_kolicina
FROM narocnik
LEFT OUTER JOIN
narocilo USING (narocnik_id)
WHERE priimek = 'Kmetec'
GROUP BY narocnik_id, ime, priimek
HAVING COALESCE(SUM(kolicina), 0) <= 3
ORDER BY stevilo;
...
FROM narocnik LEFT OUTER JOIN narocilo USING (narocnik_id)
...
narocnik_id |
ime |
priimek |
narocilo_id |
kolicina |
---|---|---|---|---|
1 | Damjan | Kmetec | 1 | 2 |
1 | Damjan | Kmetec | 2 | 1 |
2 | Aljaž | Kmetec | NULL |
NULL |
3 | Špela | Prezelj | 3 | 1 |
4 | Metka | Kmetec | 4 | 4 |
</span>
...
WHERE priimek = 'Kmetec'
...
narocnik_id |
ime |
priimek |
narocilo_id |
kolicina |
---|---|---|---|---|
1 | Damjan | Kmetec | 1 | 2 |
1 | Damjan | Kmetec | 2 | 1 |
2 | Aljaž | Kmetec | NULL |
NULL |
4 | Metka | Kmetec | 4 | 4 |
</span>
...
GROUP BY narocnik_id, ime, priimek
...
narocnik_id |
ime |
priimek |
narocilo_id |
kolicina |
---|---|---|---|---|
1 | Damjan | Kmetec | 1 | 2 |
2 | 1 | |||
2 | Aljaž | Kmetec | NULL |
NULL |
4 | Metka | Kmetec | 4 | 4 |
</span>
...
HAVING COALESCE(SUM(kolicina), 0) <= 3
...
narocnik_id |
ime |
priimek |
narocilo_id |
kolicina |
skupna_kolicina |
---|---|---|---|---|---|
1 | Damjan | Kmetec | 1 | 2 | 3 |
2 | 1 | ||||
2 | Aljaž | Kmetec | NULL |
NULL |
0 |
4 | 4 | 4 |
</span>
SELECT narocnik_id, ime, priimek,
COUNT(narocilo_id) AS stevilo,
COALESCE(SUM(kolicina), 0) AS skupna_kolicina
...
narocnik_id |
ime |
priimek |
stevilo |
skupna_kolicina |
---|---|---|---|---|
1 | Damjan | Kmetec | 2 | 3 |
2 | Aljaž | Kmetec | 0 | 0 |
...
ORDER BY stevilo;
narocnik_id |
ime |
priimek |
stevilo |
skupna_kolicina |
---|---|---|---|---|
2 | Aljaž | Kmetec | 0 | 0 |
1 | Damjan | Kmetec | 2 | 3 |
pripada
- film ima lahko več žanrov!zanr
.SELECT naziv, COUNT(*) AS stevilo
FROM pripada
JOIN zanr ON zanr = id
GROUP BY id, naziv
ORDER BY stevilo DESC;
film
.SELECT naziv,
AVG(ocena) AS povprecna_ocena,
AVG(dolzina) AS povprecna_dolzina
FROM film
JOIN pripada ON film.id = film
JOIN zanr ON zanr = zanr.id
GROUP BY zanr.id, naziv
ORDER BY povprecna_ocena DESC;
SELECT naslov, COUNT(*) AS st_reziserjev
FROM vloga
JOIN film ON film = id
WHERE tip = 'R'
GROUP BY naslov
HAVING st_reziserjev > 1
ORDER BY st_reziserjev DESC;
SELECT naslov, COUNT(*) AS st_reziserjev
FROM vloga
JOIN film ON film = id
WHERE tip = 'R'
GROUP BY id, naslov
HAVING st_reziserjev > 1
ORDER BY st_reziserjev DESC;
SELECT film FROM pripada
WHERE zanr = 4 AND zanr = 8;
zanr = 4
kot tudi z zanr = 8
!SELECT film FROM pripada
JOIN zanr ON zanr = id
WHERE naziv IN ('Comedy', 'Romance')
GROUP BY film
HAVING COUNT(*) = 2;
filmi
.
SELECT naslov FROM film
JOIN pripada ON film.id = film
JOIN zanr ON zanr = zanr.id
WHERE naziv IN ('Comedy', 'Romance')
GROUP BY film.id, naslov
HAVING COUNT(*) = 2;
SELECT naslov, ime FROM film
JOIN vloga ON film.id = film
JOIN oseba ON oseba = oseba.id
GROUP BY film.id, naslov, oseba.id, ime
HAVING COUNT(*) = 2;
WITH reziser AS (
SELECT * FROM vloga
WHERE tip = 'R'
),
igralec AS (
SELECT * FROM vloga
WHERE tip = 'I'
)
SELECT naslov, ime FROM reziser
JOIN igralec USING (oseba, film)
JOIN film ON film.id = film
JOIN oseba ON oseba.id = oseba;
SELECT film.*
FROM pripada AS prvi
JOIN pripada AS drugi USING (film)
JOIN film ON id = film
WHERE prvi.zanr = 4 AND
drugi.zanr = 8 AND
oznaka <> 'R'
ORDER BY film.glasovi DESC;
CREATE DATABASE baza;
CREATE TABLE tabela (
stolpec1 tip
[PRIMARY KEY] -- glavni ključ
[NOT NULL] -- prepovedana vrednost NULL
[UNIQUE] -- enolične vrednosti v stolpcu
[CHECK (pogoj)] -- pogoj, ki mora veljati za stolpec
[DEFAULT (vrednost)] -- privzeta vrednost
[REFERENCES druga_tabela(stolpec)], -- tuji ključ
[PRIMARY KEY (st1, st2, ...),] -- glavni ključ iz več stolpcev
[UNIQUE (st1, st2, ...),] -- enolične vrednosti v več stolpcih
[CHECK (pogoj),] -- pogoj, ki vključuje več stolpcev
[FOREIGN KEY (st1, st2, ...) -- tuji ključ iz več stolpcev
REFERENCES druga_tabela(s1, s2, ...),]
...
);
AUTOINCREMENT
pri SQLite.integer
ipd. - cela številanumeric(p, s)
, decimal(p, s)
, real
, … - decimalna številachar(n)
, varchar(n)
- nizi dolžine natanko/največ n
znakovtext
- poljubno besedilodate
- datum v obliki YYYY-MM-DD
time
- čas v obliki hh:mm:ss
datetime
- datum in čas v obliki YYYY-MM-DD hh:mm:ss
PRIMARY KEY
(glavni ključ) - stolpec (ali več njih), katerega vrednost enolično določa vrstico.
NULL
.NOT NULL
- vrednosti v stolpcu morajo obstajati (niso NULL
).UNIQUE
(enoličnost) - vrednosti v stolpcu (ali več njih) se ne smejo ponavljati.
NULL
se lahko pojavi tudi večkrat (če ni prepovedana z NOT NULL
)!CHECK
- pogoj, ki mora veljati v vsaki vrstici tabele.FOREIGN KEY
/REFERENCES
- tuji ključ oz. referenca na drugo tabelo.
NULL
(če niso prepovedane z NOT NULL
).AUTOINCREMENT
takoj za PRIMARY KEY
pri stolpcu tipa ìnteger
.CREATE TABLE kupec (
id_kupec integer PRIMARY KEY AUTOINCREMENT,
uporabnisko_ime text NOT NULL UNIQUE,
ime text NOT NULL,
priimek text NOT NULL,
naslov text,
datum_rojstva date CHECK (datum_rojstva < DATETIME('now', '-18 years')),
-- kupec mora biti starejši od 18 let
CHECK (LENGTH(ime) + LENGTH(priimek) <= 30)
);
CREATE TABLE narocilo (
id_narocilo integer PRIMARY KEY AUTOINCREMENT,
id_kupec integer NOT NULL REFERENCES kupec(id_kupec),
stevilo integer DEFAULT (1),
datum datetime DEFAULT (DATETIME('now'))
);
INSERT INTO tabela [(stolpec1, stolpec2, ...)]
VALUES (vrednost1, vrednost2, ...), ...;
NULL
.INSERT INTO tabela [(stolpec1, stolpec2, ...)]
SELECT ...;
SELECT
.
SELECT
je lahko poljuben (WHERE
, JOIN
, GROUP BY
, WITH
, …).INSERT INTO kupec (uporabnisko_ime, ime, priimek, datum_rojstva)
VALUES ('janez', 'Janez', 'Novak', '1978-03-24'); -- dobi id_kupec = 1
INSERT INTO kupec (uporabnisko_ime, ime, priimek, datum_rojstva)
VALUES ('micka', 'Micka', 'Kovač', '2008-07-11'); -- napaka!
INSERT INTO kupec (uporabnisko_ime, ime, priimek, datum_rojstva)
VALUES ('micka', 'Micka', 'Kovač', '1998-07-11'); -- dobi id_kupec = 2
INSERT INTO narocilo (id_kupec)
SELECT id_kupec FROM kupec
WHERE priimek = 'Novak'; -- ena vrstica, dobi id_narocilo = 1
INSERT INTO narocilo (id_kupec, stevilo)
VALUES (1, 3), (2, 4), (2, 2), (3, 6), (1, 5); -- napaka!
INSERT INTO narocilo (id_kupec, stevilo)
VALUES (1, 3), (2, 4), (2, 2), (1, 5); -- dobijo id_narocilo = 2, 3, 4, 5
UPDATE tabela
SET stolpec1 = vrednost1, stolpec2 = vrednost2, ...
WHERE pogoj;
WHERE
določa, katere vrstice bomo spremenili.
SET
povemo, kako spremenimo stolpce v izbranih vrsticah.
DELETE FROM tabela
WHERE pogoj;
WHERE
, bomo pobrisali vse vrstice!UPDATE narocilo
SET stevilo = stevilo - 1
WHERE id_kupec = 1;
DELETE FROM narocilo
WHERE stevilo < 1;
BEGIN [TRANSACTION];
END [TRANSACTION];
COMMIT;
ROLLBACK;
INSERT
/UPDATE
/DELETE
kot ena transakcija.