Sei sulla pagina 1di 4

Display the highest, lowest, sum and average UnitPrice of each Category.

Label
column as CategoryId, Maximum, Minimum, Sum and Average, respectively. Round
your results to the nearest whole number.
Select max(UnitPrice), min(UnitPrice),avg(UnitPrice),SUM(UnitPrice)
from Products
group by CategoryID

Display the highest, lowest, sum and average UnitPrice of each Category, where
highest UnitPrice lies in the range of 50$ to 100$ . Label column as CategoryId,
Maximum, Minimum, Sum and Average, respectively.
Select Categoryid, max(UnitPrice), min(UnitPrice), avg(UnitPrice), SUM(UnitPrice)
from Products
group by CategoryID
having max(UnitPrice) between 50 and 100

From customers table, Count all customers is each region where region is not null.
SELECT Region, COUNT(*) 'Customers'
FROM customers
GROUP BY Region
HAVING Region IS NOT NULL
ORDER BY Region

From customers table, Count all customers in each region whose contactname
contains manager and region is not null.
SELECT ContactTitle,Region, COUNT(Region) 'Customers'
FROM customers
where contacttitle like '%Manager%'
GROUP BY Region,ContactTitle
HAVING Region IS NOT NULL
ORDER BY Region

Write a query to display the number of ContactName with same ContactTitle. Sort
contact title in descending order.
Select ContactTitle, count(*) as People
from Customers
group by contactTitle
order by ContactTitle desc

Write a query to list no of customers with same ContactTitle if No of customers is


greater than 5. However their ContactTitle does not contain Manager. Order by
contact title in Descending order
Select ContactTitle, count(*) as People
from Customers
where ContactTitle not like '%Manager%'
group by contactTitle
having count(*) > 5
order by ContactTitle desc

Write a query that count all orders against each product id, if no of orders are
greater than 50. (Table: [Order Details])

Select ProductID,Count(orderId) as Orders


from [Order Details]
group by ProductID
having Count(orderId) >50

How many people are in each unique city in the employee table that have more
than one person in the city? Select the city and display the number of how many
people are in each if it's greater than 1.
select city, count(*)
from employees
group by city
having count(*) >1

the LIKE operator allows us to perform basic pattern-matching using wildcard


characters. For Microsoft SQL Server, the wildcard characters are defined as follows:
Wildcard

Description

_
(underscore
)

matches any single character

matches a string of one or more characters

[]

matches any single character within the specified range (e.g. [a-f]) or set
(e.g. [abcdef]).

[^]

matches any single character not within the specified range (e.g. [^a-f]) or

set (e.g. [^abcdef]).

A few examples should help clarify these rules.

WHERE FirstName LIKE '_im' finds all three-letter first names that end with
'im' (e.g. Jim, Tim).

WHERE LastName LIKE '%stein' finds all employees whose last name ends
with 'stein'

WHERE LastName LIKE '%stein%' finds all employees whose last name
includes 'stein' anywhere in the name.

WHERE FirstName LIKE '[JT]im' finds three-letter first names that end with
'im' and begin with either 'J' or 'T' (that is, only Jim and Tim)

WHERE LastName LIKE 'm[^c]%' finds all last names beginning with 'm'
where the following (second) letter is not 'c'.

Here too, we can opt to use the NOT operator: to find all of the employees whose
first name does not start with 'M' or 'A', we would write
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees
WHERE (FirstName NOT LIKE 'M%') AND (FirstName NOT LIKE 'A%')
resulting in

The ORDER BY Clause

UpperCase
LowerCase
Round

SUBSTRING
Length
DateTime()
Select upper(ProductName) from Products
Select lower(ProductName) from Products
select SUBSTRING(ProductName,2,5) from Products
Select CONCAT(ProductName,'Hello' )from Products
Select ProductName, len(ProductName) as ProductLength from Products
Select ltrim(ProductName) from Products
Select Rtrim(ProductName) from Products
Select Replace(ProductName,'a',' hello ') from Products

IN works same as or
select
select
select
select

*
*
*
*

from
from
from
from

products
products
products
products

where
where
where
where

SupplierID
SupplierID
SupplierID
SupplierID

in (1,2,3);
=1 and SupplierID =2 and SupplierID =3 ;
=1 or SupplierID =2 or SupplierID =3 ;
between 1 and 3 ;

Potrebbero piacerti anche