HAVING clause

HAVING bayonoti SQL ga qo'shilgan, chunki WHERE kalit so'zi agregat funksiyalar bilan ishlatilmaydi.

SU

SQL HAVING Bayonoti

HAVING bayonoti SQL ga qo'shilgan, chunki WHERE kalit so'zi agregat funksiyalar bilan ishlatilmaydi.

HAVING Sintaksisi

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Demo Ma'lumotlar Bazasi

Ushbu o'quv qo'llanmada Northwind namunaviy ma'lumotlar bazasidan "Customers" jadvalidan bir tanlov:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

SQL HAVING Misollari

Quyidagi SQL bayonoti har bir mamlakatdagi mijozlar sonini ro'yxatga oladi. Faqat 5 dan ortiq mijozga ega mamlakatlarni qo'shadi:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

Quyidagi SQL bayonoti har bir mamlakatdagi mijozlar sonini yuqoridan pastga tartibda ro'yxatga oladi (faqat 5 dan ortiq mijozga ega mamlakatlar):

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

Demo Ma'lumotlar Bazasi

Ushbu o'quv qo'llanmada Northwind namunaviy ma'lumotlar bazasidan "Orders" jadvalidan bir tanlov:

OrderIDCustomerIDEmployeeIDOrderDateShipperID
102489051996-07-043
102498161996-07-051
102503441996-07-082

Va "Employees" jadvalidan bir tanlov:

EmployeeIDLastNameFirstNameBirthDatePhotoNotes
1DavolioNancy1968-12-08EmpID1.picEducation includes a BA...
2FullerAndrew1952-02-19EmpID2.picAndrew received his BTS...
3LeverlingJanet1963-08-30EmpID3.picJanet has a BS degree...

Boshqa HAVING Misollari

Quyidagi SQL bayonoti 10 dan ortiq buyurtma bergan xodimlarni ro'yxatga oladi:

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;

Quyidagi SQL bayonoti "Davolio" yoki "Fuller" xodimlari 25 dan ortiq buyurtma berganligini ro'yxatga oladi:

SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller'
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;

Ushbu sahifada

Xato haqida xabar berish