0% found this document useful (0 votes)
1 views4 pages

SQL

The document outlines the creation and management of two databases: 'cvr' for student records and 'retail' for salespeople and customer data. It includes SQL commands for creating tables, inserting data, and querying information such as student details, customer ratings, and sales orders. Various SQL queries are provided to extract specific information from the databases, such as filtering by city, gender, and scores.

Uploaded by

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

SQL

The document outlines the creation and management of two databases: 'cvr' for student records and 'retail' for salespeople and customer data. It includes SQL commands for creating tables, inserting data, and querying information such as student details, customer ratings, and sales orders. Various SQL queries are provided to extract specific information from the databases, such as filtering by city, gender, and scores.

Uploaded by

puzaa7297
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Create database:

create database cvr;


2. Connect with database

use cvr;

3. view all the tables in cvr database


show tables;

Student:
Sno
Sname
marks
city
mobile
gender
create table student(sno int, sname varchar(20), marks int, city varchar(20),
mobile int, gender varchar(20));

insert into student values(1,'GUNJA SRIKANTH',90,'Hyderabad',90023223,'male');


insert into student values(2,'DASARI SUPRIYA',34,'Hyderabad',90023223,'female');
insert into student values(3,'VARKALA TEJASRI',50,'Pune',90023223,'female');
insert into student values(4,'LAVOORI VARSHITHA',78,'bglr',90023223,'female');
insert into student values(5,'EDUNURI BHAVIKA',10,'Pune',90023223,'female');
insert into student values(6,'BEJAGAM CHETHAN TEJA',54,'Pune',90023223,'male');
insert into student values(7,'C PRADEEP',95,'Hyderabad',90023223,'male');
insert into student values(8,'RACHAPALLY NIKHITHA',87,'bglr',90023223,'female');
insert into student values(9,'MANDLA NIKHITHA',82,'Hyderabad',90023223,'female');
insert into student values(10,'BATTULA RAM GOPAL',90,'Hyderabad',90023223,'male');
insert into student values(11,'BHUKYA SRIDHAR',77,'Pune',90023223,'male');
insert into student values(12,'SHIVA KUMAR',75,'Pune',90023223,'male');
insert into student values(13,'MOUNIKA',75,'Pune',90023223,'female');

mysql> select * from student;


+------+----------------------+-------+-----------+----------+--------+
| sno | sname | marks | city | mobile | gender |
+------+----------------------+-------+-----------+----------+--------+
| 1 | GUNJA SRIKANTH | 90 | Hyderabad | 90023223 | male |
| 2 | DASARI SUPRIYA | 34 | Hyderabad | 90023223 | female |
| 3 | VARKALA TEJASRI | 50 | Pune | 90023223 | female |
| 4 | LAVOORI VARSHITHA | 78 | bglr | 90023223 | female |
| 5 | EDUNURI BHAVIKA | 10 | Pune | 90023223 | female |
| 6 | BEJAGAM CHETHAN TEJA | 54 | Pune | 90023223 | male |
| 7 | C PRADEEP | 95 | Hyderabad | 90023223 | male |
| 8 | RACHAPALLY NIKHITHA | 87 | bglr | 90023223 | female |
| 9 | MANDLA NIKHITHA | 82 | Hyderabad | 90023223 | female |
| 10 | BATTULA RAM GOPAL | 90 | Hyderabad | 90023223 | male |
| 11 | BHUKYA SRIDHAR | 77 | Pune | 90023223 | male |
| 12 | SHIVA KUMAR | 75 | Pune | 90023223 | male |
| 13 | MOUNIKA | 75 | Pune | 90023223 | female |
+------+----------------------+-------+-----------+----------+--------+

1. Display student details whose city is Pune

ans: select * from student where city='Pune';


2. Display student details whose city is Hyderabad or Pune

ANS: Select * from student where city='Hyderabad' or city='Pune';

3. Display female students from Hyderabad and male students from Pune and all the
students from bglr.

select * from student where gender='female and city='Hyderabad' or gender='male'


and city='Pune' or city=bglr;

4. Display students details


---> from Hyderabad city, male students who scored more than 70 and female who
score more than 50.
---> from Pune only male students

select * from student city='Hyderabad' and gender='male' and marks>=70 or


gender='female' and marks>=50 and city='Hyderabad' or gender='male' and
city='Pune';

5. Display students details whose score between 50 and 80 and city should be
Hyderabad or bglr.

select * from student where marks between 50 and 80 and city in


('Hyderabad','bglr');

6.Display student details whose name starts with S and ends with A

select * from student where sname like "S%A";

7.Display number of students in each city

select city, count(*) as "NO of stds" from students group by city;

8.Best city in education based on score.

select city, avg(marks) as "avg score" from students group by city order by "avg
score" desc limit 1;

9.Top score in each city

select city,max(marks) as top_score from student group by city order by max(marks)


desc;

10. Display toper details in Hyderabad.

select * from student where city='Hyderabad' order by marks desc limit 1;

create database retail;


use retail;

CREATE TABLE salespeople (


snum INT NOT NULL,
sname VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
comm DECIMAL(4,2) NOT NULL,
PRIMARY KEY (snum)
);
INSERT INTO salespeople VALUES (1001, 'Peel', 'London', 0.12);
INSERT INTO salespeople VALUES (1002, 'Serres', 'San Jose', .13);
INSERT INTO salespeople VALUES (1004,'Motika', 'London', .11);
INSERT INTO salespeople VALUES (1007,'Rifkin', 'Barcelona', .15);
INSERT INTO salespeople VALUES (1003,'AxelRod', 'New York', .10);
INSERT INTO salespeople VALUES (1005,'Fran', 'London', .26);
CREATE TABLE customer (
cnum INT NOT NULL,
cname VARCHAR(30) NOT NULL,
city VARCHAR(30) NOT NULL,
rating int not null,
snum int NOT NULL,
PRIMARY KEY (cnum),
FOREIGN KEY (snum) REFERENCES salespeople(snum));

INSERT INTO customer VALUES (2001, 'Hoffman', 'London',100, 1001);


INSERT INTO customer VALUES (2002,'Giovanni', 'Rome', 200, 1003);
INSERT INTO customer VALUES (2003,'Liu','San Jose',200,1002);
INSERT INTO customer VALUES (2004,'Grass', 'Berlin', 300,1002);
INSERT INTO customer VALUES (2006,'Clemens', 'London', 100, 1001);
INSERT INTO customer VALUES(2008,'Cisneros','San Jose',300, 1007);
INSERT INTO customer VALUES (2007,'Pereira', 'Rome', 100 ,1004);

Q1: Find number of customers for each salespeople


Q2: Best Salesperson based on customer rating
Q3: To whom we need to fire from salespeople team.
Q4: Who has less customer rating.
Q5. Display salespeople who has customers from same city.
Q6. Find the names and numbers of all salespeople who had more than one customer.
Q7.Find customers in San Jose who have a rating above 200.
Q8. List all customers with ratings above San Jose’s average.

CREATE TABLE orders (


onum INT NOT NULL,
amt DECIMAL(7,2) NOT NULL,
odate Date NOT NULL,
cnum int NOT NULL,
PRIMARY KEY (onum),
FOREIGN KEY (cnum) REFERENCES customer(cnum)
);

INSERT INTO orders VALUES (3001, 18.69, '1996-03-10', 2008);


INSERT INTO orders VALUES (3003, 767.19, '1996-10-03', 2001);
INSERT INTO orders VALUES (3002, 1900.10, '1996-10-03', 2007);
INSERT INTO orders VALUES (3005, 5160.45, '1996-10-03', 2003);
INSERT INTO orders VALUES (3006, 1098.16, '1996-10-03', 2008);
INSERT INTO orders VALUES (3009, 1713.23, '1996-10-04', 2002);
INSERT INTO orders VALUES (3007, 75.75, '1996-10-04', 2002);
INSERT INTO orders VALUES (3008, 4723.00, '1996-10-05', 2006);
INSERT INTO orders VALUES (3010, 1309.95, '1996-10-06', 2004);
INSERT INTO orders VALUES (3011, 9891.88, '1996-10-06', 2006);

You might also like