Topshiriq: Har bir talaba o’ziga berilgan predmet soha mavzusi bo’yicha Order by,
Group by operatorlaridan foydalanib so’rovlar yaratishi va natijasi bilan ko’rsatib o’tishi
kerak.
10-Laboratoriya ishi
Mavzu
:
HAVING
standart so’zidan foydalanib so’rov yaratish.
Ishdan maqsad:
HAVING
standart so’zidan foydalanish bo`yicha ko`nikmaga ega bo`lish.
Masalani qo`yilishi:
Berilgan predmet soha ma`lumotlar bazasidagi barcha ob`yektlarni
ustida Having standart so’zidan foydalanib amallarini bajarish.
Uslubiy ko`rsatmalar
: SQL – HAVING operatori
HAVING buyrug'i guruh yordamida amalga oshirilgan guruhlash natijalarini filtrlash
imkoniyatini beradi.
Yozuvlarni tanlash uchun shart qo'yadigan WHERE buyrug’i bilan vazifasi o’xshash.
HAVING
sintaksisi
GROUP
BY
maydon
HAVING
shart;
HAVING
buyrug’iga misollar.
Barcha misollar ushbu WORKERS jadvalida bo'ladi:
id
kod
name
Ism
age
yosh
salary
oylik
1
Дима
23
100
2
Петя
23
200
3
Вася
23
300
4
Коля
24
1000
5
Иван
24
2000
6
Кирилл
25
1000
Misol №1
Ushbu misolda
GROUP BY HAVING
operatorisiz ishlatilgan:
SELECT
age,
SUM
(salary)
as
sum
FROM
workers
GROUP
BY
age
age
yosh
sum
summa
23
600
24
3000
25
1000
Endi,
HAVING
shartdan foydalanib, biz faqat umumiy ish haqi 1000 dan katta yoki unga
teng bo'lgan satrlarni qoldiramiz:
SELECT
age,
SUM
(salary)
as
sum
FROM
workers
GROUP
BY
age
HAVING
sum>=
1000
age
yosh
sum
summa
24
3000
25
1000
Misol №2
Hisoblash funksiyasi (
COUNT
) dan foydalanib, guruhdagi yozuvlar sonini hisoblang
(HAVING
dan foydalanmasdan):
SELECT
age,
COUNT
(*)
as
count
FROM
workers
GROUP
BY
age
age
возраст
count
количество
23
3
24
2
25
1
Endi, mavjud shartdan foydalanib, biz faqat satrlar soni ikkitadan kam yoki unga teng
bo'lgan guruhlarni qoldiramiz:
SELECT
age,
COUNT
(*)
as
count
FROM
workers
GROUP
BY
age
HAVING
count<=
2
age
yosh
count
soni
24
2
25
1
Shunga o'xshash effektni quyidagi buyruq yordamida olish mumkin:
SELECT
age,
COUNT
(*)
as
count
FROM
workers
GROUP
BY
age
HAVING
count
IN
(
1
,
2
);
BETWEEN buyrug'idan ham foydalanishingiz mumkin:
SELECT
age,
COUNT
(*)
as
count
FROM
workers
GROUP
BY
age
HAVING
count
BETWEEN
1
AND
2;
Misol №3
Quyidagi yozuvlarni o'z ichiga olgan CUSTOMERS jadvalini ko'rib chiqing.
SELECT
ID, NAME, AGE, ADDRESS, SALARY
FROM
CUSTOMERS
GROUP BY
age
HAVING COUNT
(age) >= 2;
Ko'rinib turibdiki, HAVING dagi barcha buyruqlar WHERE uchun ham mavjud.
Topshiriq: Har bir talaba o’ziga berilgan predmet soha mavzusi bo’yicha HAVING
operatoridan foydalanib so’rovlar yaratishi va natijasi bilan ko’rsatib o’tishi kerak.
10-Laboratoriya ishi
Mavzu: UNION, INTERSECT VA MINUS standart so’zidan foydalanish.
Ishdan maqsad:
UNION, INTERSECT VA MINUS
standart so’zidan foydalanish bo`yicha
ko`nikmaga ega bo`lish.
Masalani qo`yilishi:
Berilgan predmet soha ma`lumotlar bazasidagi barcha ob`yektlarni
ustida
UNION, INTERSECT VA MINUS
standart so’zidan foydalanib amallarini bajarish.
Uslubiy ko`rsatmalar
: SQL – UNION operatori
UNION
sintaksisi
SELECT * FROM table1 UNION SELECT * FROM table2;
Misol №1
SELECT
*
FROM
SHAXS
UNION SELECT
*
FROM
SHAXS2;
UNION ALL
sintaksisi
select
*
from
SHAXS
union
all
select
*
from
SHAXS2
;
select * from SHAXS union all select * from SHAXS2;
INTERSECT
sintaksisi
INTERSECT - Faqat birinchi va ikkinchi to'plamda mavjud bo'lgan yozuvlarni to'playdi va
qaytaradi.
select
*
from
shaxs
intersect
select
*
from
shaxs2
;
Mysql da
INTERSECT operatoriga alternative IN
SELECT shaxs.name
-> FROM shaxs
rows in set (0.00 sec)
Proyeksiya operatori
Proektsion operatsiya () tanlash operatsiyasi kabi ishlaydi va faqat bitta munosabat bilan
ishlaydi va yangi aloqani belgilaydi, unda faqat operatsiyada ko'rsatilgan atributlar (ustunlar) va
ularning qiymatlari mavjud.
SELECT DISTINCT
A4, A3 from R3;
SELECT DISTINCT name from shaxs;
+--------+
| name |
+--------+
| Aziz |
| Akrom |
| Vali |
| Ali |
| Nodira |
+--------+
DEKART KO’PAYTMA amali
SELECT * from shaxs, shaxs2;
+------+--------+------+--------+
30 rows in set (0.00 sec)
MINUS operatori
Ushbu operator MySqlda mavjud emas, uning o’rnida quyidagi
UNION, INTERSECT VA MINUS operatoridan foydalanib so’rovlar yaratish
UNION ALL
sintaksisi
INTERSECT
sintaksisi
INTERSECT - Faqat birinchi va ikkinchi to'plamda mavjud bo'lgan yozuvlarni to'playdi va
qaytaradi.
select
*
from
shaxs
intersect
select
*
from
shaxs2
;
Mysql da
INTERSECT operatoriga alternative IN
Topshiriq: Har bir talaba o’ziga berilgan predmet soha mavzusi bo’yicha union,
intersect, minus operatorlaridan foydalanib so’rovlar yaratishi va natijasi bilan ko’rsatib
o’tishi kerak.
Do'stlaringiz bilan baham: |