Informatics Practices
Consider the tables given below and answer the questions that follow:
Table: EVENT
EventId | Event | NumPerformers | CelebrityID |
---|---|---|---|
101 | Birthday | 10 | C102 |
102 | Promotion Party | 20 | C103 |
103 | Engagement | 12 | C102 |
104 | Wedding | 15 | C104 |
Table: CELEBRITY
CelebrityID | CelebrityName | Phone | FeeCharged |
---|---|---|---|
C101 | Faiz Khan | 99101XXXXX | 200000 |
C102 | Sanjay Kumar | 89346XXXXX | 250000 |
C103 | Neera Khan Kapoor | 98116XXXXX | 300000 |
C104 | Reena Bhatia | 70877XXXXX | 100000 |
(a) Name the Primary keys in both the tables and the Foreign key in 'Event' table. Can NumPerformers (Number of performers) be set as the Primary key? Give reason.
(b) How many rows will be present in the Cartesian join of the above-mentioned two tables?
(c) Write the commands in SQL:
- To display EventId, Event name, CelebrityId for only those events that have more than 10 performers.
- To display CelebrityId and names of celebrities who have "Khan" anywhere in their names.
- To display names of celebrities and fee charged for those celebrities who charge more than 200000.
SQL Queries
3 Likes
Answer
(a) In the EVENT
table, the primary key is EventId
, and in the CELEBRITY
table, the primary key is CelebrityID
. The foreign key in the EVENT
table is CelebrityID
, which references the CelebrityID
in the CELEBRITY
table.
NumPerformers
cannot be set as the primary key because primary keys must uniquely identify each row in the table. NumPerformers
can have duplicate values for different events, as multiple events can have the same number of performers.
(b) The EVENT
table has 4 rows and the CELEBRITY
table has 4 rows. So, the Cartesian join will have: 4 × 4 = 16 rows.
(c)
1.
SELECT EVENTID, EVENT, CELEBRITYID
FROM EVENT
WHERE NUMPERFORMERS > 10;
Output
+---------+-----------------+-------------+
| EVENTID | EVENT | CELEBRITYID |
+---------+-----------------+-------------+
| 102 | PROMOTION PARTY | C103 |
| 103 | ENGAGEMENT | C102 |
| 104 | WEDDING | C104 |
+---------+-----------------+-------------+
2.
SELECT CELEBRITYID, CELEBRITYNAME
FROM CELEBRITY
WHERE CELEBRITYNAME LIKE '%Khan%';
Output
+-------------+-------------------+
| CELEBRITYID | CELEBRITYNAME |
+-------------+-------------------+
| C101 | FAIZ KHAN |
| C103 | NEERA KHAN KAPOOR |
+-------------+-------------------+
3.
SELECT CELEBRITYNAME, FEECHARGED
FROM CELEBRITY
WHERE FEECHARGED > 200000;
Output
+-------------------+------------+
| CELEBRITYNAME | FEECHARGED |
+-------------------+------------+
| SANJAY KUMAR | 250000 |
| NEERA KHAN KAPOOR | 300000 |
+-------------------+------------+
Answered By
2 Likes
Related Questions
Consider the CUSTOMERS table having the following records:
Table: CUSTOMERS
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Bengaluru 4500.00 7 Muffy 24 Indore 10000.00 (a) Write an SQL query to display all records in ascending order of name.
(b) Write an SQL query to display all records in descending order of name.
(c) Write an SQL query to display all records in ascending order of name and descending order of age.
(d) Write an SQL query to display maximum salary.
(e) Write an SQL query to display minimum salary.
(f) Write an SQL query to display total number of records.
(g) Write an SQL query to display average salary.
(h) Write an SQL query to display total salary of all the persons.
(i) Write an SQL query to display names of those persons whose salary is greater than the average salary.
(j) Write an SQL query to display details of those persons whose age is less than the average age.
Consider the following tables WORKER and PAYLEVEL and answer the questions:
Table: WORKER
ECODE NAME DESIG PLEVEL DOJ DOB 11 Sachin Patel Supervisor P001 2004-09-13 1985-08-23 12 Chander Nath Operator P003 2010-02-22 1987-07-12 13 Fizza Operator P003 2009-06-14 1983-10-14 15 Ameen Ahmed Mechanic P002 2006-08-21 1984-03-13 18 Sanya Clerk P002 2005-12-19 1983-06-09 Table: PAYLEVEL
PLEVEL PAY ALLOWANCE P001 26000 12000 P002 22000 10000 P003 12000 6000 (a) To display details of all workers in descending order of DOB.
(b) To display the PLEVEL and number of workers in that PLEVEL.
(c) To display the PLEVEL and number of workers in that PLEVEL whose pay is greater than 15000.
(d) To display NAME and DESIG of those workers, whose PLEVEL is either P001 or P002.
(e) Give the output of the following SQL queries:
- SELECT COUNT(PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;
- SELECT MAX(DOB), MIN(DOJ) FROM WORKER;
Consider the following structure of TEACHER and STUDENT table:
Table: TEACHER
TeacherID TName City Subject Qualification Designation Pay Table: STUDENT
StdID Name FName Stream TeacherID Write the SQL commands to get the following:
(a) Show the name of students enrolled in Science stream.
(b) Count the number of students in Commerce stream.
(c) Count the number of teachers in each designation.
(d) Display the maximum pay of teacher who is teaching English.
(e) Display the names of students who are taught by "Anand Mathur".
(f) Display the names and designations of teachers who are teaching a student named "Amit".
(g) Find out the name of the teacher who is getting the highest pay.
(h) Find out the cities of teachers who are teaching Maths.
(i) Find out the name of teacher who is getting the lowest salary among PGTs.
(j) Display the list of students who are taught by PGTs only.
Consider the following table and answer the questions that follow:
Table: TEACHER
ID Name Department Hiredate Category Gender Salary 1 Tanya Nanda SocialStudies 1994-03-17 TGT F 25000 2 Saurabh Sharma Art 1990-02-12 PRT M 20000 3 Nandita Arora English 1980-05-16 PGT F 30000 4 James Jacob English 1989-10-16 TGT M 25000 5 Jaspreet Kaur Hindi 1990-08-01 PRT F 22000 6 Disha Sehgal Math 1980-03-17 PRT F 21000 8 SonaliMukherje Math 1980-11-17 TGT F 24500 Write the command/output for the following:
(a) To display all information about the teacher of PGT category.
(b) To list the names of female teachers of Hindi department.
(c) To list names, departments and date of hiring of all the teachers in ascending order of date of joining.
(d) To count the number of teachers in English Department.
(e) Display the department and hire date of all the female teachers whose salary is more than 25000.
(f) Display the list of teachers whose name starts with J.
(g) SELECT COUNT(*) FROM TEACHER WHERE Category = 'PGT';
(h) SELECT AVG(Salary) FROM TEACHER GROUP BY Gender;