You are on page 1of 5

Advance Database Management

System
Subject code:22CAP605
Student name: Tarun Saraswat.
UID:22MCA20528
Section/Group:1/A

Experiment no. 2

Experiment 1.2
You are given three tables: Students, Friends and Packages. Students contains two
columns: ID and Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best
friend). Packages contains two columns: ID and Salary (offered salary in $ thousands per month).
Write a query to output the names of those students whose best friends got offered a higher salary
than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed
that no two students got same salary offer.

Queries made:
mysql> create table students(id int, name varchar(30));
mysql> create table packages (id int, salary int);
mysql> create table friends (id int, friend_id int);
[To create the tables ]
mysql> insert into students values (1,'Mukul');
mysql> insert into students values (2,'Vishnu');
mysql> insert into students values (3,'Bhanu');
[To insert data in the table]
mysql> select* from students; [To show the table]

mysql> insert into packages values (1,10000);


mysql> insert into packages values (2,12000);
mysql> insert into packages values (3,8000);
[To insert data in the table]

mysql> select* from packages; [To show the table]

mysql> insert into friends values (1,2);


mysql> insert into friends values (2,3);
mysql> insert into friends values (3,1);
[To insert data in the table]

mysql> select* from friends; [To show the table]

select s.id ,s.NAME,p.salary, f.friend_id, p2.salary as friend_salary from students s


inner JOIN packages p ON s.id = p.id left join friends f ON f.id = s.id left join
packages p2 ON f.friend_id = p2.id where p.salary <= p2.salary order BY s.id;
[To show the final table according to the requirement ]

Screenshots of outputs:
Conclusion:
I learned about creating Database, creating tables in the database, Defining
different columns in the tables, showing the table of the database by making
some quarries, using inner join and sub query and filtering the data according to
the required conditions from different tables.

You might also like