9-Lesson SQL Commands
9-Lesson SQL Commands
SQL commands
Class will start with refreshing the previous class with QA…. (30)
SQL Commands
SQL is a Standard Query Language for storing, manipulating and retrieving data in
databases.
#Notes……..
Suppose you have a table “Customers” with data under columns CustomerID,
CustomerName, ContactName, Address, City, PostalCode, and Country
Below are the most common QUERY or COMMANDS to get output as enquired,
from the above table “Customer”
SQL (pronounced as SEQUEL), queries are not case sensitive, but for easy
understanding the COMMANDS are written in caps.
For practice we can download mysql work bench, which is a bit long process. There is
separate sheet for installing mysql work bench.(Cr-22)
-- is used for comment, which sql will not count in to action
But for a short cut method, we shall practice on cloudra.
3. Select unique values from duplicate in Country column in the Customers table.
5. Select all records where the City is not "Berlin" from Customer table
SELECT * FROM Customers
WHERE NOT City = ‘Berlin’;
6. Select all records where the CustomerID column has the value 32.
SELECT * FROM Customers
WHERE CustomerID = 32;
7. Select all records where the City column has the value Berlin & PostalCode is 12209
SELECT * FROM Customers
WHERE City = ‘Berlin’
AND PostalCode = 12209;
8. Select all records where the City is Berlin & also City is London.
SELECT * FROM Customers
WHERE City = ‘Berlin’
OR City = ‘London’;
9. Select all records from the Customers table, sort the result alphabetically by the
column City.
SELECT * FROM Customers
ORDER BY City;
10. Select all records from the Customers table, sort the result reversed
alphabetically by the column City.
SELECT * FROM Customers
ORDER BY City DESC;
11. Select all records from the Customers table, sort the result alphabetically first
by country then City.
SELECT * FROM Customers
ORDER BY Country, City;
12. Select all records from the Customers where the PostalCode column is empty.
SELECT * FROM Customers
WHERE PostalCode IS NULL;
2
13. Select all records from the Customers where the PostalCode column is NOT empty.
SELECT * FROM Customers
WHERE PostalCode IS NOT NULL;
14. Update the City column of all records in the Customers table.
UPDATE Customers
SET City = ‘Oslo’
WHERE City_id = 7;
15. Delete all the records from the Customers table where the Country is Norway
DELETE FROM Customers
WHERE COUNTRY = ‘Norway’;5
17. Select all records where the value of the City column starts with the letter "a".
SELECT * FROM Customers
WHERE City LIKE ‘a%’;
18. Select all records where the value of the City column ends with the letter "a".
SELECT * FROM Customers
WHERE City LIKE ‘%a’;
19. Select all records where the value of the City column contains the letter "a".
SELECT * FROM Customers
WHERE City LIKE ‘%a%’;
20. Select all records where the value of the City column starts with letter "a" and
ends with the letter "b".
SELECT * FROM Customers
WHERE City LIKE ‘a%b’;
21. Select all records where the value of the City does NOT start with "a".
SELECT * FROM Customers
WHERE City NOT LIKE ‘a%’;
3
22. Select all records where the second letter of the City is an "a".
SELECT * FROM Customers
WHERE City LIKE ‘_a%’;
23. Select all records where the first letter of the City is an "a" or a "c" or an "s".
24. Select all records where first letter of City starts with anything from "a" to "f".
25. Select all records where the first letter of City is NOT an "a" or a "c" or an "f".
SELECT * FROM Customers
WHERE City LIKE ‘[!acf]%’;
26. Renaming column title of Customers table, make an ALIAS of the PostalCode
column, the column should be called Pno instead.
SELECT CustomerName,
Adress,
PostalCode As Pno
FROM Customers;
27. When displaying the Customers table, refer to the table as Consumers instead of
Customers.
SELECT *
FROM Customers As Consumers;
The below commands are from a new table “Products” with column………
28. Select the minimum price from the Price column of table “Products”
SELECT MIN(Price)
FROM products;
29. Select the minimum price from the Price column of table “Products”
4
SELECT MAX(Price)
FROM products
30. Find number of record with price 18
SELECT COUNT(*)
FROM Products
WHERE Price = 18;
32. Calculate the sum of all the Price in the Products table.
SELECT SUM(Price)
FROM Products;
33. Select all the records where the value of the Price is between 10 and 20.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
34. Select all the records where the value of the Price is NOT between 10 and 20.
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
35. Select all the records where the ProductName column is alphabetically between
'Geitost' and 'Pavlova'.
SELECT * FROM Products
WHERE ProductName BETWEEN 'Geitost' AND 'Pavlova';
(INNER) JOIN: Returns records that have matching values in both tables
(self/multiple/cross/natural)
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the
right table, unmatched data will result in null.
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the
left table, unmatched data will be resulted as null.
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
5
SELF JOIN (WITHIN SINGLE TABLE is one kind of inner join, which works over one table
and create a new table with existing data in a new structure or schema as intended.
SQL course in 3 hour by mosh (along with mysql work bench installation)
https://www.youtube.com/watch?v=7S_tz1z_5bA
Install mysql workbench with Cr-22 sheet, and practice commands. No need to connect and
create or insert data.
Operator: =, >, <, <=, >=, !=, <>
6