0% found this document useful (0 votes)
156 views9 pages

SQL Queries for Customer Data Analysis

The document contains examples of SQL queries using SELECT, UPDATE, DELETE, WHERE, ORDER BY, LIMIT, LIKE, and other clauses. Some examples select all records, specific fields, or records matching certain criteria. Other examples update, delete, sort, limit results, or use wildcards in LIKE queries.

Uploaded by

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

SQL Queries for Customer Data Analysis

The document contains examples of SQL queries using SELECT, UPDATE, DELETE, WHERE, ORDER BY, LIMIT, LIKE, and other clauses. Some examples select all records, specific fields, or records matching certain criteria. Other examples update, delete, sort, limit results, or use wildcards in LIKE queries.

Uploaded by

Puchki Arpita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) SELECT * FROM table_name;

2) selects the "CustomerName" and "City" columns from the


"Customers" table:
SELECT CustomerName, City FROM Customers;
3) selects all (and duplicate) values from the "Country" column
in the "Customers" table:
SELECT Country FROM Customers;
4) selects only the DISTINCT values from the "Country"
column in the "Customers" table:
SELECT DISTINCT Country FROM Customers;
5) SELECT COUNT(DISTINCT Country) FROM Customers;
6) SELECT Count(*) AS DistinctCountries
7) FROM (SELECT DISTINCT Country FROM Customers);
8) selects all the customers from the country "Mexico", in the
"Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico';
9) SELECT * FROM Customers
WHERE CustomerID=1;

10) selects all fields from "Customers" where country is


"Germany" AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
11) statement selects all fields from "Customers" where city is
"Berlin" OR "München":
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
12) selects all fields from "Customers" where country is NOT
"Germany"
SELECT * FROM Customers
WHERE NOT Country='Germany';
13) selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR "München" (use
parenthesis to form complex expressions):
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City=
'München');
14) selects all fields from "Customers" where country is NOT
"Germany" and NOT "USA":
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
15) selects all customers from the "Customers" table, sorted by
the "Country" column:
SELECT * FROM Customers
ORDER BY Country;
16) selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
SELECT * FROM Customers
ORDER BY Country DESC;
17) selects all customers from the "Customers" table, sorted by
the "Country" and the "CustomerName" column:
SELECT * FROM Customers
ORDER BY Country, CustomerName;
18) selects all customers from the "Customers" table, sorted
ascending by the "Country" and descending by the
"CustomerName" column:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
19) updates the first customer (CustomerID = 1) with a new
contact person and a new city.
UPDATE Customers
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt'
WHERE CustomerID = 1;
20) update the contactname to "Juan" for all records where
country is "Mexico":
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
21) deletes the customer "Alfreds Futterkiste" from the
"Customers" table:
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
22) delete all rows in a table without deleting the table. This
means that the table structure, attributes, and indexes will
be intact:
DELETE * FROM table_name;
23) select the first three records from the "Customers" table:
SELECT TOP 3 * FROM Customers;
The following SQL statement shows the equivalent example
using the LIMIT clause:
SELECT * FROM Customers
LIMIT 3;
The following SQL statement shows the equivalent example
using ROWNUM:
SELECT * FROM Customers
WHERE ROWNUM <= 3;
24) selects the first 50% of the records from the "Customers"
table:
SELECT TOP 50 PERCENT * FROM Customers;
25) selects the first three records from the "Customers" table,
where the country is "Germany":
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
or
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;

or
SELECT * FROM Customers
WHERE Country='Germany' AND ROWNUM <= 3;

26) finds the price of the cheapest product:


SELECT MIN(Price) AS SmallestPrice
FROM Products;
27) finds the price of the most expensive product:
SELECT MAX(Price) AS LargestPrice
FROM Products;
28) finds the number of products:
SELECT COUNT(ProductID)
FROM Products;
29) finds the average price of all products:
SELECT AVG(Price)
FROM Products;

The SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.

There are two wildcards used in conjunction with the LIKE


operator:
% - The percent sign represents zero, one, or multiple
characters
_ - The underscore represents a single character.

Here are some examples showing different LIKE operators


with '%' and '_' wildcards:
LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that


starts with "a"

WHERE CustomerName LIKE '%a' Finds any values that


ends 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 starts with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that


starts with "a" and ends with "o"

1) selects all customers with a CustomerName starting


with "a":
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
2) The following SQL statement selects all customers
with a CustomerName ending with "a":
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
a. The following SQL statement selects all
customers with a CustomerName that have "or"
in any position:
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
3) The following SQL statement selects all customers
with a CustomerName that have "r" in the second
position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
4) The following SQL statement selects all customers
with a CustomerName that starts with "a" and are at
least 3 characters in length:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a_%_%';
5) The following SQL statement selects all customers
with a ContactName that starts with "a" and ends
with "o":
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
6) The following SQL statement selects all customers
with a CustomerName that does NOT start with "a":
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';

ANSWER:

1)select * from supplier

2)select customerName,city from Customer

3)select city from Customer

4)select distinct city from customer

5) select count(distinct city) from customer

6) select count (*)categoryName from category

8)select * from customer where city='Ashokenagar'

9)select * from customer where customerID=23


10)select * from category where description='mylife' and
categoryID='10';

11)select * from customer where city='Chadpara' or


city='Ashokenagar'

12)select * from customer where not city='Ashokenagar'

13)select * from supplier where city='Ashokenagar'and


(suppliername='Arpita' or suppliername='Aindrila');

14)select * from customer where not city ='Ashokenagar'


and not city='Chadpara'

15)select * from customer order by city

16)select * from customer order by city desc

17)select * from customer order by city,customername

18)select * from customer order by city asc,customerid


desc

19)update customer set customername='Alfred',City=


'Frankfurt'where customerid='3'

20)update customer set customername='juan'where


city='Ashokenagar'

21)delete from customer where customername='Alfred'

22)delete from product

23)select * from customer where rownum<=3

24)select top 50 persent * from customer/select * from


customer where rownum<=50
25)select * from customer where city='kolkata' and
rownum<=3

26)select min(price) as smallestprice from product

27)select max(price) as largestprice from product

28)select count(productid) from product

29)select avg(price) from product

SQL LIKE OPERATOR

1)select * from customer where customername like 'M%'

2)select * from customer where customername like '%a'

3)select * from customer where customername like '%or%'

4)select * from customer where customername like '_r%'

5)select * from customer where customername like 'j_%_%'

6)select * from customer where customername like 'P%a'

7)select * from customer where customername not like


'a%'

SAME AS EMPLOYEE TABLE

1)select * from Employee1 where firstname like 'A%'

2)select * from Employee1 where firstname like '%a'

3)select * from Employee1 where lastname like '%or%'

4)select * from Employee1 where firstname like '_r%'

5)select * from Employee1 where firstname like 'A_%_%'

6)select * from Employee1 where firstname like 'A%o'


7)select * from Employee1 where firstname not like 'A%'

You might also like