You are on page 1of 12

SQL

TABLES USED:

Customers
customer_id first_name last_name age country
1 George Thomas 31 USA
2 Robert Froster 22 UK
3 Rijo Abraham 22 UK
4 Joseph Reiner 25 UK
5 Lucas luke 28 USA

EMP

empno ename job salary


1234 TREVER Clerk 120000
2376 CHRISTOPHER Salesman 100000
975 LLMAR Clerk 180000

Orders
order_id item amount customer_id
1 Box 20 4
2 Pencil 5 4
3 Box 20 3
4 Eraser 4 1
5 Sharpener 6 2

23
SQL COMMANDS.

COMMAND 1: Creation of a table.


create table emp(empno integer,ename char(20),job char(10),salary integer);

OUTPUT:

empno Ename job salary


Empty

COMMAND 2: Inserting values into it.


insert into emp values('1234',' TREVER ','Clerk','120000');
insert into emp values('2376',' CHRISTOPHER ','Salesman','100000');
insert into emp values('0975','LLMAR','Clerk','180000');

OUTPUT:

empno ename job salary


1234 TREVER Clerk 120000
2376 CHRISTOPHER Salesman 100000
975 LLMAR Clerk 180000

COMMAND 3: adding a column to the table:


Alter table emp add Phone no int(10);

OUTPUT:

empno ename job salary Phone no


1234 TREVER Clerk 120000
1234 TREVER Clerk 120000
2376 CHRISTOPHER Salesman 100000
975 LLMAR CLERK 190000

23
COMMAND 4: Query to display information of people with age greater than 22 from table
customers.

Customers
customer_id first_name last_name age country
1 George Thomas 31 USA
2 Robert Froster 22 UK
3 Rijo Abraham 22 UK
4 Joseph Reiner 25 UK
5 Lucas luke 28 USA

Select * from customers where age>22;


OUTPUT:

customer_id first_name last_name age country


1 George Thomas 31 USA
4 Joseph ReIner 25 UK
5 Lucas luke 28 USA

COMMAND 5: To select distinct countries from table customers.

select distinct country from customers;


OUTPUT:

country
USA
UK

COMMAND 6: To count the number of rows in the table emp.


23
select count(*) from emp;
OUTPUT:

count(*)
4

23
COMMAND 7: To display the information of customers whose age is between 20 to 25.

select * from customers where age between '22' and '25'

OUTPUT:

customer_id first_name last_name age country


2 Robert Froster 22 UK
3 Rijo Abraham 22 UK
4 Joseph Reiner 25 UK

COMMAND 8: To display information of customers whose name starts with ‘R’

select * from customers where last_name like "A%"


OUTPUT:

customer_id first_name last_name age country


3 Rijo Abraham 22 UK

COMMAND 9: To display the minimum age of customer.

23
Select MIN(age) from customers;
OUTPUT:

MIN(age)
22

COMMAND 10: To select average age of customers.

Select AVG(age) from customers;


OUTPUT:

AVG(age)
25.6

COMMAND 11: select first_name from customers group by age;


OUTPUT:

23
first_name
Robert
Joseph
Lucas
George

23
COMMAND 12:
select item from orders group by amount;
OUTPUT:

item
Eraser
Pencil
Sharpener
Box

COMMAND 13:
select first_name from customers where first_name is NOT NULL;
OUTPUT:

first_name
George
Robert
Rijo
Joseph
Lucas

COMMAND 14:

23
delete from customers where age = 31;
OUTPUT:

customer_id first_name last_name age country


2 Robert Froster 22 UK
3 Rijo Abraham 22 UK
4 Joseph Reiner 25 UK
5 Lucas luke 28 USA

COMMAND 15:
select first_name , last_name from customers where age like _5;
OUTPUT:

4 Joseph Reiner 25 UK

23
COMMAND 16:
select customer_id from customers group by age having age>22;
OUTPUT:

customer_id
4
5

COMMAND 17:
select * from customers where first_name like "%t%";
OUTPUT:

2 Robert Froster 22 UK

COMMAND 18:
23
select customer_id* age from customers;
OUTPUT:

customer_id* age
44
66
100
140

COMMAND 19:
select 15*2;
OUTPUT:

15*4
60

23
COMMAND :20
Select first_name,age,item from customers,orders;
OUTPUT:
first_name age item
George 31 Box
George 31 Pencil
George 31 Box
George 31 Eraser
George 31 Sharpener
Robert 22 Box
Robert 22 Pencil
Robert 22 Box
Robert 22 Eraser
Robert 22 Sharpener
Rijo 22 Box
Rijo 22 Pencil
Rijo 22 Box
Rijo 22 Eraser
Rijo 22 Sharpener
Joseph 25 Box
Joseph 25 Pencil
Joseph 25 Box
Joseph 25 Eraser
Joseph 25 Sharpener
Lucas 28 Box
Lucas 28 Pencil
Lucas 28 Box
Lucas 28 Eraser
Lucas 28 Sharpener

23

You might also like