0% found this document useful (0 votes)
6 views28 pages

DBI - Chapter6 - 2024 - Examples

6. DBI_Chapter6_2024_Examples

Uploaded by

baotochi87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views28 pages

DBI - Chapter6 - 2024 - Examples

6. DBI_Chapter6_2024_Examples

Uploaded by

baotochi87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

sql server

1
Wildcard characters

% (Percent): Represents zero or more characters.


_ (Underscore): Represents a single character.
[] (Square Brackets): Represents any single character
within the brackets (like a set of characters)
[^] (Caret inside square brackets): Represents any single
character not within the brackets.
2
SELECT * FROM STUDENT WHERE Name LIKE 'A%';
-- Finds any name starting with 'A'

SELECT * FROM STUDENT WHERE Name LIKE '_an';


-- Finds names like 'Dan', 'Jan', etc.

SELECT * FROM STUDENT WHERE Name LIKE '[CD]an';


-- Finds names like 'Dan' or 'Can'

SELECT * FROM STUDENT WHERE Name LIKE '[^A]an';


-- Finds names that do not start with 'A', like 'Dan', 'Can', etc.

3
eX:

4
5
6
IN
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

7
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

8
BETWEEN ... AND
GROUP BY

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

12
HAVING

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

14
15
The following SQL statement lists the number of customers in
each country. Only include countries with more than 5
customers:

16
17
The following SQL statement lists the employees that
have registered more than 10 orders:

18
Lists if the employees "Davolio" or "Fuller" have
registered more than 25 orders:

19
EXISTS
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(
SELECT column_name
FROM table_name
WHERE condition
);

20
21
The following SQL statement returns TRUE and lists the
suppliers with a product price less than 20:

SELECT SupplierName
FROM Suppliers a
WHERE EXISTS
( SELECT ProductName
FROM Products b
WHERE b.SupplierID = A.supplierID AND Price < 20
);
ANY/ ALL

SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);

Note:
The operator must be (=, <>, !=, >, >=, <, or <=).
ALL Syntax With WHERE or HAVING

SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
CASE

CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater
than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);

You might also like