SQL Questions 4 Tables

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT

001 Monika Arora 100000 2014-02-20 HR

002 Niharika Verma 80000 2014-06-11 09 Admin

003 Vishal Singhal 300000 2014-02-20 HR

004 Amitabh Singh 500000 2014-02-20 Admin

005 Vivek Bhati 500000 2014-06-11 Admin

006 Vipul Diwan 200000 2014-06-11 Account

007 Satish Kumar 75000 2014-01-20 Account

008 Geetika Chauhan 90000 2014-04-11 Admin

Sample Table – Bonus

WORKER_ID BONUS_DATE BONUS_AMOUNT

001 2016-02-20 5000

004 2016-06-11 3000

002 2016-02-20 4000

003 2016-02-20 4500

005 2016-06-11 3500

1. Display first name, salary of all workers who belong to accounts department

2. Display all details of table Bonus in the increasing order of bonus amount.

3. Display all details of workers table whose name starts with v in the ascending order

4. Display how many departments are in the worker table?

5. Display all workers in the descending order of doj.

6. Display first name , DEPARTMENT , BONUS AMOUNT OF ALL EMPLOYEES WHERE WORKER_ID
OF WORKER TABLE IS EQUAL WORKER_ID OF BONUS TABLE
OUTPUT

1. SELECT DEPARTMENT ,MIN(SALARY),MAX(SALARY) FROM WORKER GROUP BY DEPARTMENT;

2. SELECT DISTINCT(DEPARTMENT) FROM WORKER;

3. SELECT Max(bonus),min(bonus),from bonus;

4. Select w.worker_id,w.salary, b.bonus from worker w, bonus b where w.worker_id=b.worker_id;

SALESMAN
salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12

CUSTOMER
customer_id | cust_name | city | grade | salesman_id
-------------+----------------+------------+-------+------------
-
3002 | Nick Rimando | New York | 100 | 5001
3007 | Brad Davis | New York | 200 | 5001
3005 | Graham Zusi | California | 200 | 5002
3008 | Julian Green | London | 300 | 5002
3004 | Fabian Johnson | Paris | 300 | 5006
3009 | Geoff Cameron | Berlin | 100 | 5003
3003 | Jozy Altidor | Moscow | 200 | 5007
3001 | Brad Guzan | London | | 5005

1) Display all details of those salesmen who are IN paris city

SELECT * FROM SALESMAN WHERE CITY=’PARIS’;

2) Display name, city, commission as per the ascending order of commission

SELECT NAME,CITY,COMMISSION FROM SALESMAN ORDER BY


COMMISSION ASC;
3) Display name, commission , cust name, grade of tables salesman and
customer.

SELECT S.NAME,S.COMMISSION, C.CUST_NAME,C.GRADE FROM


SALESMAN S, CUSTOMER C WHERE
S.SALESMAN_ID=C.SALESMAN_ID;

4) Display all those customers who do not belong to grade 100.

SELECT * FROM CUSTOMER WHERE GRADE IN(300,200);

SELECT * FROM CUSTOMER WHERE GRADE <>100;

SELECT * FROM CUTOMER WHERE GRADE NOT IN(100);

5) Display salesman id along with their customer_id and city.

SELECT SALESMAN_ID, CUSTOMER_ID, CITY FROM CUSTOMER;

6) Count howmany distinct cities in customer table

SELECT COUNT(DISTINCT(CITY)) FROM CUSTOMER;

7) Display all those salemen whose name has second letter ‘a’

SELECT * FROM SALESMAN WHERE NAME LIKE ‘_a%’;

1) Select customer_id, customer_name where city not in(“London”,”new


York”)
customer_id | cust_name | city
-------------+----------------+-----

3005 | Graham Zusi | California |


3004 | Fabian Johnson | Paris
3009 | Geoff Cameron | Berlin |
3003 ozy Altidor | Moscow |
2)Select count(distinct(grade)) from customer

Count(distinct(grade))

3)Select name,city, commission*12 “annual commission” from salesman


where salesmanid=5007
salesman_id | name | city | commission*12

5007 | Paul Adam | Rome | 1.56


4) select city, max(commission), min(commission), count(commission)
from salesman group by city having commission>=0.13;

City max(commission) min(commission) count(commission)

New York 0.15 0.15 1

Paris 0.14 0.13 2

5) select city, max(commission), min(commission), count(commission)


from salesman group by city having count( commission)>=2;

City max(commission) min(commission) count(commission)

Paris 0.14 0.13 2


Consider the following tables CAR & CUSTOMER and answer(a) and (b) parts of this question.

Table:-CAR

Ccode CarName Make Color Capacity Charges

501 A-Star Suzuki RED 3 14

503 Indigo Tata SILVER 3 12

502 Innova Toyoto WHITE 7 15

509 SX4 Suzuki SILVER 4 14

510 C Class Mercedes RED 4 35

Table:-CUSTOMER

CCode Cname Ccode

1001 Hemant 501


Sahu

1002 Raj Lal 509

1003 Feoza Shah 503

1004 Ketan Dhal 502

a)Write SQL commands for the following statements: (5)

i) To display the names of all the silver colored cars


a) SELECT CNAME FROM CAR WHERE COLOUR = ‘SILVER’;
ii) To display the names of car, make and capacity of cars in
descending order of their sitting capacity.
A)SELECT CARNAME, MAKE ,CAPACITY FROM CAR ORDER BY
CAPACITY DESC;
iii) To display the highest charges at which a vehicle can be
hired from CAR.
A)SELECT MAX(CHARGES) FROM CAR;
iv) To display the customer name and the corresponding name
of the cars hired by them.)
A)SELECT C.CNAME, R.CCODE FROM CUSTOMER C AND CAR R
WHERE R.CCODE=C.CCODE;
v) To display all the details of car whose Ccode=501
A)SELECT * FROM CAR WHERE CCODE=501;
VI DISPLAY ALL DETAILS OF CAR WHERE CHARGES ARE I N
BETWEEN 20 AND 40 INCLUSIVE BOTH
A)SELECT * FROM CAR WHERE CHARGES BETWEEN 20 AND 40;
VII DISPLAY CARNAME, CAPACITY, CHARGE+10 FROM CAR
TABLE IF CAPACITY IS MORE THAN OR EQUAL TO 3
A)SELECT CARNAME, CAPCITY, CHARGES+10 “UPDATED
CHARGES’” FROM CAR WHERE CAPACITY>=3;
b) Give the output of the following SQL queries: (5)

i)SELECT COUNT(DISTINCT Make) FROM CAR;


COUNT(DISTINCT Make)
A) 4
ii) SELECT MAX(Charges), MIN(Charges) FROM ;
A)
MAX(Charges) MIN(Charges)
35 12

iii) SELECT CAPACITY , MIN(CHARGES), MAX(CHARGES) FROM


CAR GROUP BY CAPACITY HAVING COUNT(CAPACITY)>=2;
A)CAPACITY MIN(CHARGES) MAX(CHARGES)
4 14 35
3 12 14
A) iv) SELECT CarName FROM CAR WHERE Capacity=4;
CARNAME
SX4
CCLASS
A) v) SELECT * FROM CAR WHERE Ccode in (501,510);
Ccode CarName Make Color Capacity Charges

501 A-Star Suzuki RED 3 14

510 C Class Mercedes RED 4 35

You might also like