You are on page 1of 6

LESSON-09

SQL commands
 Class will start with refreshing the previous class with QA…. (30)

Today’s topics: Lesson plan

1. SQL Commands (120)


2. SQL Joins (just brief description) (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.

1. Get all the columns from the Customers table.

SELECT * FROM Customers;

2. Select the City column from the Customers table.

SELECT City FROM Customers;

3. Select unique values from duplicate in Country column in the Customers table.

SELECT DISTINCT Country FROM Customers;


1
4. Select all records where the City column has the value "Berlin" from Customer table
SELECT * FROM Customers
WHERE City = ‘Berlin’;

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;

Similarly for changing multiple column of specific row


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;(use PK)

15. Delete all the records from the Customers table where the Country is Norway
DELETE FROM Customers
WHERE COUNTRY = ‘Norway’;5

16. Delete all the records from the Customers table


DELETE FROM Customers;

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".

SELECT * FROM Customers


WHERE City LIKE ‘[acs]%’;

24. Select all records where first letter of City starts with anything from "a" to "f".

SELECT * FROM Customers


WHERE City LIKE ‘[a-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;

31. Calculate the average price of all products.


SELECT AVG(Price)
FROM Products;

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';

Different Types of SQL JOINs


Here are the different types of the JOINs in SQL:

 (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.

 Command to connect mysql in cloudera


mysql –u root –p
cloudera
mysql>

 CREATE Database myfirsttutorial; # To create a database

 USE myfirsttutorial; # to use myfirsttutorial data base

 Command to create table

CREATE TABLE Customers(CustomerId int NOT NULL, CustomerName varchar(20),


ContactName varchar(20), Address varchar(50), City varchar(20), PostalCode
int(8), Country varchar(20));

 DESCRIBE Customers; # to view the schema of the table Customers

 Command to insert values


 INSERT INTO Customers VALUES (101, ‘Ravi’, ‘Ravi’, ‘102 ny street’, ‘ny’,
11102, ‘USA’), (102, ‘Avi’, ‘Avi’, ‘103 nj street’, ‘nj’, 11103, ‘USA’),
(103, ‘Ram’, ‘Ram’, ‘104 ct street’, ‘ct’, 11104, ‘USA’);

Link to practice SQL


https://www.w3schools.com/sql/sql_and_or.asp

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

You might also like