Amaliy mashg’ulotlarida berilgan topshiriqlar


CustomerID CustomerName


Download 395.12 Kb.
bet7/20
Sana15.11.2023
Hajmi395.12 Kb.
#1777301
1   2   3   4   5   6   7   8   9   10   ...   20
Bog'liq
SQL command 29-11-2021

CustomerID

CustomerName

ContactName

Address

City

PostalCode

Country

1



Alfreds Futterkiste

Maria Anders

Obere Str. 57

Berlin

12209

Germany

2

Ana Trujillo Emparedados y helados

Ana Trujillo

Avda. de la Constitución 2222

México D.F.

05021

Mexico

3

Antonio Moreno Taquería

Antonio Moreno

Mataderos 2312

México D.F.

05023

Mexico

4



Around the Horn

Thomas Hardy

120 Hanover Sq.

London

WA1 1DP

UK

5

Berglunds snabbköp

Christina Berglund

Berguvsvägen 8

Luleå

S-958 22

Sweden

ORDER BY DESC Example


The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country DESC;

ORDER BY Several Columns Example


The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;

ORDER BY Several Columns Example 2


The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

SQL LIKE Examples


The following SQL statement selects all customers with a CustomerName starting with "a":

LIKE Operator

Description

WHERE CustomerName LIKE 'a%'

Finds any values that start with "a"

WHERE CustomerName LIKE '%a'

Finds any values that end with "a"

WHERE CustomerName LIKE '%or%'

Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%'

Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%'

Finds any values that start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%'

Finds any values that start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o'

Finds any values that start with "a" and ends with "o"

Example
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

The SQL COUNT(), AVG() and SUM() Functions


COUNT() Example


The following SQL statement finds the number of products:
Example
SELECT COUNT(ProductID)
FROM Products;

AVG() Example


The following SQL statement finds the average price of all products:
Example
SELECT AVG(Price)
FROM Products;

SUM() Example


The following SQL statement finds the sum of the "Quantity" fields in the "OrderDetails" table:
Example
SELECT SUM(Quantity)
FROM OrderDetails;


IN Operator Examples


The following SQL statement selects all customers that are located in "Germany", "France" or "UK":

Example


SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

BETWEEN Example


The following SQL statement selects all products with a price between 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SQL GROUP BY Examples


The following SQL statement lists the number of customers in each country:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
The following SQL statement lists the number of customers in each country, sorted high to low:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

8 Laboratoriya ishlariga topshiriq


SQL HAVING Examples
The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL statement lists the number of customers in each country, sorted high to low (Only include countries with more than 5 customers):


Example


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



Download 395.12 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   ...   20




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling