SQL Revision
SQL Revision
3. The __________clause of SELECT query allows us to select only those rows in the results that
satisfy a specified condition.
        (a) Where              (b) from        (c) having              (d) like
4. Which of the following function is used to FIND the largest value from the given data in
MYSQL?
       (a) MAX ()            (b) MAXIMUM ()         (c) LARGEST ()         (c) BIG ()
5. The data types CHAR (n) and VARCHAR (n) are used to create _______ and _______ types of
string/text fields in a database.
        (a)      Fixed, equal (b) Equal, variable (c) Fixed, variable (d) Variable, equal
7. Which command is used for cleaning up the environment (sql with Python)?
       (a) my.close        (b) is.close           (c) con.close        (d) mycon.close
12. Name the host name used for signing in the database.
      (a) localhost         (b) localpost          (c) localcost            (d) none of the above
14. Which is the subset of SQL commands used to manipulate database structure including tables?
       (a) Data Definition Language (DDL)            (b) Data Manipulation Language (DML)
       (c) Both (a) and (b)                          (d) None
       Which of the names will not be displayed by the below given query?
       SELECT name FROM employee WHERE employee_id>5009;
       (a) Amit, Sumit     (b) Sumit, Arpit        (c) Arpit            (d) Amit, Arpit
20. Pick the correct username used for logging in database (sql with Python).
       (a) root               (b) local             (c) directory          (d) host
21. Aggregate functions can be used in the select list or the _____ clause of a select statement.
They cannot be used in a ______ clause.
       (a) Where, having     (b) Having, where        (c) Group by, having (d) Group by, where
22. Select correct SQL query from below to find the temperature in increasing order of all cites.
        (a) SELECT city FROM weather ORDER BY temperature;
        (b) SELECT city, temperature FROM weather;
        (c) SELECT city, temperature FROM weather ORDER BY temperature;
        (d) SELECT city, temperature FROM weather ORDER BY city;
23. In SQL, which command is used to SELECT only one copy of each set of duplicable rows
        (a) SELECT DISTINCT                         (b) SELECT UNIQUE
        (c) SELECT DIFFERENT                        (d) All of the above
24. Which of the following is a SQL aggregate function?
      (a) LEFT                (b) AVG              (c) JOIN                   (d) LEN
26. An attribute in a relation is foreign key if it is the _________key in any other relation.
       (a) Candidate            (b) Primary               (c) Super           (d) Sub
27. Which of the following sublanguages of SQL is used to query information from the data base
and to insert tuples into, delete tuples from, and modify tuples in the database?
        (a) DML (Data Manipulation Language)
        (b) DDL (Data Definition Language)
        (c) Query
        (d) Relational Schema
29. Which of the following is not a legal method for fetching records from database from within
Python?
      (a) fetchone()          (b) fetchtwo()         (c) fetchall()        (d) fetchmany()
31. Which of the following attributes can be considered as a choice for primary key?
      (a)Name                 (b)Street             (c) Roll No            (d) Subject
33. What SQL statement do we use to display the record of all students whose last name contains 5
letters ending with “A”?
         (a) SELECT * FROM STUDENTS WHERE LNAME LIKE ‘_ _ _ _A’;
         (b) SELECT * FROM STUDENTS WHERE LNAME LIKE ‘ _ _ _ _ _’;
         (c) SELECT * FROM STUDENTS WHERE LNAME LIKE ‘ ????A’;
         (d) SELECT * FROM STUDENTS WHERE LNAME LIKE ‘*A’;
40. To open a connector to Mysql database, which statement is used to connect with mysql?
       (a) Connector        (b) Connect            (c) password          (d) username
41. If column “Marks” contains the data set {25, 35, 25, 35, 38}, what will be the output after the
execution of the given query?
        SELECT MARKS (DISTINCT) FROM STUDENTS;
        (a) 25. 35. 25. 35. 38 (b) 25, 25, 35, 35   (c) 25, 35, 38         (d) 25, 25, 35, 35
42. Which connector is used for linking the database with Python code?
      (a) MySQL-connector                           (b) YesSQL: connector
      (c) PostSQL: connector                        (d) None of the above
43. If column “Salary” contains the data set {1000, 15000, 25000, 10000, 15000}, what will be the
output after the execution of the given query?
        SELECT SUM(DISTINCT SALARY) FROM EMPLOYEE;
        (a)75000               (b) 25000             (c) 10000            (d) 50000
44. SQL applies conditions on the groups through _____ clause after groups have been formed,
      (a) Group by           (b) With              (c) Where             (d) Having
45. To execute all the rows from the result set, which method is used?
       (a) fetchall            (b) fetchone          (c) fetchmany          (d) none of the above
46. What is the meaning of “HAVING” clause is SELECT query?
      (a) To filter out the summary groups        (b) To filter out the column groups
      (c) To filter out the row and column values (d) None of the mentioned
48. Which operator tests column for the absence of data (i.e., NULL value) ?
      (a) EXISTS operator           (b) NOT operator
      (c) IS operator               (d) None of these
50. Which SQL function is used to count the number of rows in a SQL query?
      (a) COUNT ()          (b) NUMBER ()         (c) SUM ()           (d) COUNT (*)
51. With SQL, how can you return the number of not null record in the Project field of “Students”
table?
       (a) SELECT COUNT (Project) FROM Students
       (b) SELECT COLUMNS (Project) FROM Students
       (c) SELECT COLUMNS (*) FROM Students
       (d) SELECT COUNT (*) FROM Students
53. All aggregate functions except _______ ignore null values in their input collection.
        (a) Count (attribute) (b) Count (*)        (c) Avg                 (d) Sum
55. What will be the order of the data being sorted after the execution of given query
      SELECT * FROM STUDENT ORDER BY ROLL_NO;
      (a)Custom Sort           (b) Descending         (c) Ascending          (d) None of the above
56. Where and Having clauses can be used interchangeably in SELECT queries?
      (a) True              (b) False             (c) Only in views  (d) With order by
57. A______ is property of the entire relation, which ensures through its value that each tuple is
unique in a relation.
       (a) Rows              (b) Key                 (c) Attribute          (d) fields
58. The operation whose result contains all pairs of tuples from the two relations, regardless of
whether their attribute values match.
       (a) Join                (b) Cartesian product (c) Intersection       (d) Set difference
60. The pattern ‘- - - ’ matches any string of ________ three character. ‘- - - %’ matches any string
of ____ three characters.
       (a) Atleast, Exactly (b) Exactly, Atleast (c) Atleast, All             (d) All, Exactly
Q1. Name the command/clause which is used to display the records in ascending or descending
order.
Q2. Give example of any two DML commands.
Q3. What is the purpose of SQL?
Q4. What is primary key?
Q5. Which command is used to display a list of already existing tables?
Q6. Which command is used to change the structure of table?
Q7. Which command is used to change the data of the table?
Q8. Which command is used to delete data of the table?
Q9. Which command delete the structure of table?
Q10. Identify the DDL and DML commands from the following:
    Create, Delete
Q11. Which clause is used with aggregate functions? (Group by/ Where)
Q12. What do you mean by candidate key?
Q13. Correct the error in the following query.
    Select * from RECORD where Rname = %math%;
Q14. What is max () function in SQL?
Q15. What do you mean by degree and cardinality of table?
Q16. Expand DDL and DML
Q17. Which command is used to increase the salary of workers in table salary? (Update / Alter)
Q18. Name the command used to see the structure of table.
Q19. Which aggregate function is used to find sum of column in a table?
Q20. What is the difference between having and where clause?
Q21. Name an aggregate function in SQL which return the average of numeric values.
Q22. What is the use of “like” in SQL?
Q23. Correct the following statement:
     Delete table data;
Q24. What do you mean by aggregate function?
Q25. Write two wild card characters which are used with like operator?
Q26. Duplication of record is called ____________
Q27. What is the difference between char and varchar?
=====================*====================*========================
                            True/False Questions
1. The condition in a WHERE clause in a SELECT query can refer to only one value
2. SQL provides the AS keyword, which can be used to assign meaningful column names to
    the results of queries using the SQL built-in functions.
3. The rows of the result relation produced by a SELECT statement can be sorted but only by
    one column.
4. SQL is a programming language.
5. SELECT DISTINCT is used if a user wishes to see duplicate columns in a query.
6. The HAVING clause acts like a WHERE clause, but it identifies groups that meet a
    criterion, rather than rows.
7. The qualifier DISTINCT must be used in an SQL statement when we want to Eliminate
    duplicate rows.
8. DISTINCT and its counterpart, ALL, can be used more than once in a SELECT statement.
9. DISTINCT and its counterpart, ALL, can be used together on single field in a SELECT
    statement.
10. SUM, AVG, MIN and MAX can only be used with numeric columns.
11. The SQL statement: SELECT salary + Comm AS Total FROM Emp; adds two fields salary
    and comm from each row together and lists the results in a column named Total.
                           -------ANSWER -------
         OBJECTIVE TYPE QUESTIONS /MULTIPLE CHOICE QUESTIONS
1    (a)Structure Query Language      21   (b)Having, where               41   (c)25,35,38
                                            -------ANSWER -------
                       VERY SHORT ANSWER QUESTIONS (1 MARKS EACH)
            ANS                  Q.N. ANS              Q.N. ANS
     Q.N.
     1    order by clause              2         Insert , Delete            3      SQL is structured query
                                                                                   language. It is a standard
                                                                                   language of all the
                                                                                   RDBMS
     4       A field which is          5         show tables;               6       Alter
            unique for each and
            every record in table is
            called primary key.
     7      Update                     8         Delete                     9      Drop
                                           -------ANSWER -------
                                       Fill in the blanks
1     Structured                           11    NOT IN                      21   Group/row/ aggregation
                                                                                  function
6 WHERE 16 COUNT
7 ORDER BY 17 GROUP BY
8 DESC 18 Fetchall()
9     AND                                  19    Connection
10    IN                                   20    Select
                                   -------ANSWER -------
                                   True and False
    1                F                11                T                21             F
2 T 12 T 22 F
3 F 13 F 23 F
4 F 14 F 24 T
5 F 15 T 25 F
6 T 16 T 26 T
7 T 17 T 27 T
8 F 18 F 28 F
9 F 19 T 29 F
10 T 20 F 30 F
It returns one result per row It returns one result for multiple rows.
                                          FACULTY
         F_ID      Fname              Lname           Hire_date        Salary
         102       Amit               Mishra          12-10-1998       12000
         103       Nitin              Vyas            24-12-1994       8000
         104       Rakshit            Soni            18-5-2001        14000
         105       Rashmi             Malhotra        11-9-2004        11000
         106       Sulekha            Srivastava      5-6-2006         10000
                                          COURSES
                 C_ID F_ID                  Cname                Fees
                 C21       102              Grid Computing       40000
                 C22       106              System Design        16000
                 C23       104              Computer Security    8000
                 C24       106              Human Biology        15000
                 C25       102              Computer Network     20000
                 C26       105              Visual Basic         6000
   i) Select COUNT(DISTINCT F_ID) from COURSES;
   ii) Select MIN(Salary) from FACULTY,COURSES where COURSES.F_ID =
        FACULTY.F_ID;
   iii) Select avg(Salary) from FACULTY where Fname like ‘R%’
Q.2Write output for (i) & (iii) basedon a table COMPANY and CUSTOMER.
              COMPANY
      CID          NAME               CITY         PRODUCTNAME
      111          SONY              DELHI                 TV
      222          NOKIA            MUMBAI               MOBILE
      333          ONIDA             DELHI                 TV
      444          SONY             MUMBAI               MOBILE
      555      BLACKBERRY           MADRAS               MOBILE
      666          DELL              DELHI       LAPTOP
                                        CUSTOMER
                CUSTID     NAME                 PRICE     QTY      CID
  Q.3 Write output for (i) to (iii) based on the tables ‘Watches’ and ‘Sale’ given below.
                                           Table: Watches
           Watchid        Watch_Name              Price        Type           Qty_Store
           W001           HighTime                10000        Unisex         100
           W002           LifeTime                15000        Ladies         150
           W003           Wave                    20000        Gents          200
           W004           HighFashion             7000         Unisex         250
           W005           GoldenTime              25000        Gents          100
                                             Table: Sale
                       Watchid              Qty_Sold           Quarter
                       W001                 10                 1
                       W003                 5                  1
                       W002                 20                 2
                       W003                 10                 2
                       W001                 15                 3
                       W002                 20                 3
                       W005                 10                 3
                       W003                 15                 4
  i. select quarter, sum(qty_sold) from sale group by quarter;
  ii. select watch_name,price,type from watches w, sale s wherew.watchid!=s.watchid;
  iii. select watch_name, qty_store, sum(qty_sold), qty_store-sum(qty_sold) “Stock” from
watches
      w, sale s where w.watchid=s.watchid group by s.watchid;
Q.4 Write the output for SQL queries (i) to (iii), which are based on the table:
Employees
                                    Employees
     Empid      Firstname       Lastname        Designation           City         Salary
       010         Ravi          Kumar           Manager              GZB          75000
       105         Harry         Waltor          Manager              GZB          65000
       152         Sam            Tones          Director             Paris        80000
        215         Sarah         Ackerman          Manager                Upton         75000
        244         Manila        Sengupta             Clerk           New Delhi         50000
        300         Robert          Samuel             Clerk          Washington         45000
        335          Ritu          Tondon              Clerk               GZB           40000
        400         Rachel            Lee           Salesman           New York          32000
        441          Peter        Thompson          Salesman               Paris         28000
 (i) Select Designation , count(*) from Employees Group by Designation Having
     count(*)>=3;
  (ii) Select Max (salary), Min(Salary) from Employees Where City in (‘GZB’,
‘Paris’);
  (iii) Select Firstname, Lastname from Employees where Firstname like ‘R%’;
Q.5 Write output for queries (i) to (iii), which are based on the table:
Books.
                  Book_id    Book_name       Author_name       Publisher   Price   Qty
                  C0001      Fast Cook       Lata Kapoor       EPB         355     5
                  F0001      The Tears       William hopkin    NIL         650     20
                  T0001      My First Py     Brain& Brooke     EPB         350     10
                  T0002      Brain works     A.W. Rossaine     TDH         450     15
                  F0002      Thunderbolts    Anna Roberts      NIL         750     5
   i.   Select Count(Publisher) from Books;
 ii.    Select Max(Price) from books where qty >=15;
 iii.   Select count(distinct publishers) from books where Price>=400;
                                            ANSWERS
ANS .1 (i) 4 (ii) 6000 (iii) 12500
Ans.2
    (i) Count(*) CITY
     3           DELHI
     2           MUMBAI
     1           MADRAS
   (ii) MIN (PRICE) -50000
   MAX (PRICE) -7000
  (iii) AVG (QTY)
  11
Ans.3
(i) Quarter sum(qty_sold)
 1          15
 2         30
 3          45
 4         15
(ii) watch_name price type
    HighFashion 7000 Unisex
(iii)
watch_name qty_store qty_sold       Stock
 HighTime       100     25             75
 LifeTime      150      40            110
 Wave           200      30            170
GoldenTime 100           10            90
Ans4.
   (i)  Manager 3
        Clerk    3
  (ii) 80000 28000
  (iii) Ravi   Kumar
        Robert Samuel
        Ritu   Tondon
        Rachel Lee
 Ans .5
   (i) 3   (ii)650    (iii)TDH
Table: GRADUATE
  S.N            NAME              STIPEN             SUBJECT              AVERAG         DI
  O.                                  D                                       E           V
   1            KARAN                400             PHYSICS                 68            1
   2           DIVAKAR               450           COMPUTER SC               68            1
    3            DIVYA                 300           CHEMISTRY                62          2
    4                ARUN              350             PHYSICS                63          1
    5            SABINA                500         MATHEMATICS                70          1
    6                JOHN              400           CHEMISTRY                55          2
    7           ROBERT                 250           PHYSICS                  64          1
    8           RUBINA                 450         MATHEMATICS                68          1
    9            VIKAS                 500         COMPUTER SC                62          1
   10.           MOHAN                 300         MATHEMATICS                57          2
           (a) List the names of those students who have obtained DIV 1 sorted by NAME.
          (b) Display a report, listing NAME, STIPEND, SUBJECT and amount of stipend
              received in a year assuming that the STIPEND is paid every month.
          (c) To count the number of students who are either PHYSICS or COMPUTER SC
              graduates.
          (d) To insert a new row in the GRADUATE table:
                     11, “KAJOL”, 300, “COMPUTER SC”, 75, 1
     (e) Display Name of the students whose average is more than 65.
Q.2 Write SQL commands for (a) to (e) on the basis of table CLUB.
                            Table: CLUB
       COAC           COAC            AGE SPORTS                  DATEOFAP PAY               SE
       H ID           H                                                 P                     X
                      NAME
             1.        KUKREJA          35       KARATE             27/03/1997    1000        M
             2.         RAVINA          34       KARATE             20/01/1998    1200        F
             3.         KARAN           34        SQUASH            19/02/1998    2000        M
             4.         TARUN           33    BASKETBALL            01/01/1998    1500        M
             5.          ZUBIN          36      SWIMMING            12/01/1998     750        M
             6.         KETAKI          36      SWIMMING            24/02/1998     800        F
             7.         ANKITA          39        SQUASH            20/02/1998    2200        F
             8.         ZAREEN          37       KARATE             20/02/1998    1100        F
             9.          KUSH           41      SWIMMING            13/01/1998     900        M
            10.        SHAILYA          37    BASKETBALL            19/02/1998    1700        M
         (a) To show all information about the swimming coaches in the club.
         (b) To list names of all coaches with their date of appointment (DATOFAPP) in
              descending order.
         (c) To display a report, showing coachname, pay, age and bonus (15% of pay) for all
              the coaches.
         (d) To insert in a new row in the CLUB table with the following data:
              11, “PRAKASH”, 37, “SQUASH”, {25/02/98}, 2500, “M”
         (e ) Display Coachname ,Sports,Pay from the table .
      3. Write SQL command for (a) to (e ) on the basis of tables INTERIORS and
      NEWONES.
                           Table: INTERIORS
  NO       ITEMNAME           TYPE          DATEOFSTOCK             PRICE          DISCOUNT
   1          Red rose      Double bed         23/02/02              32000             15
   2         Soft touch      Baby cot          20/01/02              9000              10
   3       Jerry’s home      Baby cot          19/02/02              8500              10
   4        Rough wood      Office Table       01/01/02              20000             20
   5       Comfort zone     Double bed         12/01/02              15000             20
   6         Jerry look      Baby cot          24/02/02              7000              19
   7         Lion king      Office Table       20/02/02              16000             20
   8        Royal tiger         Sofa           22/02/02              30000             25
   9        Park sitting        Sofa           13/12/01              9000              15
   10         Dine Paradise     Dining Table         19/02/02             11000             15
                                    Table: NEWONES
  NO         ITEMNAME             TYPE       DATEOFSTOCKS               PRICE          DISCOUNT
  11          White wood        Double bed       23/03/03                 20000            20
  12           James 007           Sofa          20/02/03                 15000            15
  13           Tom look          Baby cot        21/02/13               7000               10
           (a) To show all information about the sofas from the INTERIORS table.
           (b) To list the ITEMNAME which are priced at more than 10,000 from the
INTERIORS table.
           ( c) To list ITEMNAME and TYPE of those items, in which DATEOFSTOCK
is before
                 22/01/02 from the INTERIERS table in the descending order of
ITEMNAME.
           (d )To display ITEMNAME and DATEOFSTOCK of those items, in which the
discount
                 Percentage is more than 15 from INTERIORS table.
          ( e )To count the number of items, whose type is “Double Bed” from INTERIOR table.
       4. Write SQL command for (a) to (e) on the bases of tables FURNITURE AND ARRIVALS.
                                     Table: FURNITURE
          (a) To show all information about the baby cots from the FURNITURE table.
          (b) To list the ITEMNAME which are priced at more than 15000 from the
              FURNITURE table.
          (c) To list ITEMNAME AND TYPE of those items, in which DATEOFSTOCK is
              before 22/01/02from the FURNITURE table in descending order of
              ITEMNAME.
          (d) To display ITEMNAME and DATEOFSTOCK of those items, in which
              the DISCOUNTpercentage is more than 25 from FURNITURE table.
          (e) To insert a new row in the ARRIVALS table with
              the following data:14, “Velvet touch”, Double bed”,
              {25/03/03}, 25000, 30
5. Write SQL commands for (a) to (e) on the basis of Teacher relation given below:
Relation Teacher
 No.         Name               Ag         Department             Date of            Salary       Sex
                                 e                                 join
  1.         Jugal              34           Computer            10/01/97            12000        M
  2.       Sharmila             31            History            24/03/98            20000        F
  3.       Sandeep              32            Maths              12/12/96            30000        M
  4.       Sangeeta             35            History            01/07/99            40000        F
  5.        Rakesh              42            Maths              05/09/97            25000        M
  6.        Shyam               50            History            27/06/98            30000        M
  7.        Shiv Om             44           Computer            25/02/97            21000        M
  8.       Shalakha             33            Maths              31/07/97            20000        F
                                           TABLE ACCESSORIES
                                     No           Name        Price              ID
                                     A01          Mother Board
                                                  12000
                                                  S01
                                     A02          Hard Disk
                                                  5000
                                                  S01
                                     A03          Keyboard                500            S02
                                     A04          Mouse                   300            S01
                                     A05          Mother Board
                                                  13000
                                                  S02
                                     A06          Keyboard
                                                  400
                                                  S03
                                     A07          LCD                     6000           S04
                                     T08          LCD                     5500           S05
                                     T09          Mouse                   350            S05
                                     T10          Hard Disk               4500           S03
     Write the SQL queries:
            (i)      To display Name and Price of all the accessories in ascending order of their
                     Price.
            (ii)     To display Id and SName of all Shop in Nehru Place.
            (iii)    To display Minimum and Maximum Price of each Name of accessories.
            (iv)     To display Name, Price of all accessories and their respective
                     SName where they are available.
            (v)      To display all Sname in descending order.
  7. Consider the following table GARMENT and FABRIC, Write SQL commands for the
  statements (i) to (v)
                                           TABLE GARMENT
                                  TABLE FABRIC
                                     FCODE               TYPE
                                     F 04                POLYSTER
                                     F 02                COTTON
                                     F 03                SILK
                                     F01                 TERELENE
(i) To display GCODE and DESCRIPTION of each GARMENT in descending order of
GCODE.
(ii) To display the details of all the GARMENT, which have READYDATE in between 08-
DEC-07 and16-JUN-08 (inclusive if both the dates).
(iii) To display the average PRICE of all the GARMENT, which are made up of
fabric with FCODE as F03.
(iv) To display fabric wise highest and lowest price of GARMENT from
GARMENT table. (Display FCODE of each GARMENT along with highest and
lowest Price).
(v) To display Gcode whose Price is more than 1000.
                                   ANSWERS:
                              CASE STUDY BASED QUESTIONS
          1.(a)   Select Name From GRADUATE Where DIV = 1 Order by Name;
          (b)      Select Name, stipend, subject, stepend *12 From
                  GRADUATE
           (c)    Select count (*) From GRADUATE
                  Where subject IN (“PHYSICS”, “COMPUTER SC”);
           (d)    Insert into GRADUATE Values (11, “KAJOL”, 300, “COMPUTER SC”,
                  75,1);
           (e )   Select name from Graduate where average>65
     5
                  (a)     SELECT * FROM Teacher WHERE Department = “History”;
                 (b)      SELECT Name FROM Teacher WHERE Department = “Hindi” and Sex =
                          “F”;
                 (c)      SELECT Name, Dateofjoin          FROM Teacher ORDER BY
                          Dateofjoin;
                  d)       SELECT Name, Salary, Age FROM Teacher
                               WHERE Age > 23 AND Sex = ‘M’;
                   (e ) SELECT COUNT (*) FROM Teacher      WHERE Age > 23;