0% found this document useful (0 votes)
40 views

SQL

The document discusses various SQL commands used to manipulate data in tables like customers, orders, and employees. It shows commands to create, insert, alter, select, count, group, and delete data from the tables. The output for each command is displayed to demonstrate the results of the SQL queries on the sample data.

Uploaded by

Joel Jose
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

SQL

The document discusses various SQL commands used to manipulate data in tables like customers, orders, and employees. It shows commands to create, insert, alter, select, count, group, and delete data from the tables. The output for each command is displayed to demonstrate the results of the SQL queries on the sample data.

Uploaded by

Joel Jose
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM 20: Write a Program to read data from data file in read mode and append the

words starting with letter ‘T’ in a given file in python.

CODE:
f=open("test.txt",'r')
read=f.readlines()
f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)

OUTPUT:
[Test program\n]
SQL
TABLES USED:

Customers
customer_id first_name last_name age country
1 John Doe 31 USA
2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE

EMP

empno ename job salary


1234 JAKE Clerk 123000
2376 CHRISTINE Saleswoman 100000
975 LAURA CLERK 190000

Orders
order_id item amount customer_id
1 Keyboard 400 4
2 Mouse 300 4
3 Monitor 12000 3
4 Keyboard 400 1
5 Mousepad 250 2
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','JAKE','Clerk','123000');
insert into emp values('2376','CHRISTINE','Saleswoman','100000');
insert into emp values('0975','LAURA','CLERK','190000');

OUTPUT:

empno ename job salary


1234 JAKE Clerk 123000
2376 CHRISTINE Saleswoman 100000
975 LAURA CLERK 190000

COMMAND 3: adding a column to the table:


Alter table emp add icode int(10);

OUTPUT:

empno ename job salary icode


1234 JAKE Clerk 123000
1234 JAKE Clerk 123000
2376 CHRISTINE Saleswoman 100000
975 LAURA CLERK 190000
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 John Doe 31 USA
2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE

Select * from customers where age>22;

OUTPUT:

customer_id first_name last_name age country


1 John Doe 31 USA
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE

COMMAND 5: To select distinct countries from table customers.

select distinct country from customers;


OUTPUT:

country
USA
UK
UAE

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

select count(*) from emp;


OUTPUT:

count(*)
4
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 Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK

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

select * from customers where last_name like "R%"


OUTPUT:

customer_id first_name last_name age country


3 David Robinson 22 UK
4 John Reinhardt 25 UK

COMMAND 9: To display the minimum age of customer.

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;

first_name
Robert
John
Betty
John

COMMAND 12:
select item from orders group by amount;

item
Mousepad
Mouse
Keyboard
Monitor

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

first_name
John
Robert
David
John
Betty

COMMAND 14:
delete from customers where age = 31;

customer_id first_name last_name age country


2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE

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

4 John Reinhardt 25 UK
COMMAND 16:
select customer_id from customers group by age having age>22;

customer_id
4
5

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

COMMAND 18:
select customer_id* age from customers;

customer_id* age
44
66
100
140

COMMAND 19:
select 15*2;

15*2
30
COMMAND :20
select first_name , age , item, amount from customers,orders;

You might also like