SQL GROUP BY, HAVING, ORDER BY

The SQL GROUP BY Statement

The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.

GROUP BY Syntax

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

Example

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
This query will show count(customerIds with same country), Countries
COUNT(CustomerID)Country
3Argentina
2Austria
2Belgium
9Brazil
3Canada

The SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

HAVING Syntax

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

Example

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
Number of Records: 5
COUNT(CustomerID)Country
9Brazil
11France
11Germany
7UK
13USA

Example

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) 5
ORDER BY COUNT(CustomerID) DESC;
Number of Records: 5
COUNT(CustomerID)Country
13USA
11France
11Germany
9Brazil
7UK

ORDER BY Statement

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example

SELECT * FROM Customers
ORDER BY Country;
Number of Records: 91
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
12Cactus Comidas para llevarPatricio SimpsonCerrito 333Buenos Aires1010Argentina
54Océano Atlántico Ltda.Yvonne MoncadaIng. Gustavo Moncada 8585 Piso 20-ABuenos Aires1010Argentina
64Rancho grandeSergio GutiérrezAv. del Libertador 900Buenos Aires1010Argentina
20Ernst HandelRoland MendelKirchgasse 6Graz8010Austria
59Piccolo und mehrGeorg PippsGeislweg 14Salzburg5020Austria
50Maison DeweyCatherine DeweyRue Joseph-Bens 532BruxellesB-1180Belgium

Example

SELECT * FROM Customers
ORDER BY Country DESC;

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
33GROSELLA-RestauranteManuel Pereira5ª Ave. Los Palos GrandesCaracas1081Venezuela
35HILARIÓN-AbastosCarlos HernándezCarrera 22 con Ave. Carlos Soublette #8-35San Cristóbal5022Venezuela
46LILA-SupermercadoCarlos GonzálezCarrera 52 con Ave. Bolívar #65-98 Llano LargoBarquisimeto3508Venezuela
47LINO-DelicatesesFelipe IzquierdoAve. 5 de Mayo PorlamarI. de Margarita4980Venezuela
32Great Lakes Food MarketHoward Snyder2732 Baker Blvd.Eugene97403USA
36Hungry Coyote Import StoreYoshi LatimerCity Center Plaza 516 Main St.Elgin97827USA
43Lazy K Kountry StoreJohn Steel12 Orchestra TerraceWalla Walla99362USA
45Let's Stop N ShopJaime Yorres87 Polk St. Suite 5San Francisco94117USA
48Lonesome Pine RestaurantFran Wilson89 Chiaroscuro Rd.Portland97219USA

























Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question