0% found this document useful (0 votes)
7 views59 pages

Informatic Practices HHW

The document contains a series of Python programming exercises and SQL queries related to data manipulation using Pandas and database operations. It includes code corrections, data frame creation, data visualization, and various SQL queries to retrieve employee information based on specific conditions. The document serves as a holiday homework assignment for a student named Vedant Aggarwal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views59 pages

Informatic Practices HHW

The document contains a series of Python programming exercises and SQL queries related to data manipulation using Pandas and database operations. It includes code corrections, data frame creation, data visualization, and various SQL queries to retrieve employee information based on specific conditions. The document serves as a holiday homework assignment for a student named Vedant Aggarwal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

VEDANT AGGARWAL S7-H

IP HOLIDAY HOMEWORK

1) The python code written below has syntactical errors. Rewrite the correct code and
underline the corrections made.

Import pandas as pd

df={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}

df= Pd.dataframe(df)

Print(df)

ANSWER

Import pandas as pd

df={"Technology":["Programming","Robotics","3DPrinting"],"Time(in months)":[4,4,3]}

df = pd.DataFrame(df)

Print(df)
2. Complete the given Python code to get the required output as: Rajasthan

import _______as pd

di = {'Corbett': 'Uttarakhand', 'Sariska': 'Rajasthan', 'Kanha': 'Madhya


Pradesh’,'Gir':'Gujarat'}

NP =_____ . Series( ______)

print(NP[ ___________])

ANSWER

import pandas as pd

di = {'Corbett': 'Uttarakhand', 'Sariska': 'Rajasthan', 'Kanha': 'Madhya Pradesh',


'Gir':'Gujarat'}

NP = pd.Series(di)

print(NP['Sariska'])
3)

import pandas as pd

rollno=[1,2,3,4,5,6,7]

_____=pd.Series(rollno) #Statement 1

print (p.head(__________ )) #Statement 2

ANSWER

import pandas as pd

rollno = [1,2,3,4,5,6,7]

p = pd.Series(rollno) # Statement 1

print(p.head()) # Statement 2

OUTPUT

0 1

1 2

2 3

3 4

4 5

5 6

6 7

dtype: int64
4) 4. Consider two objects L and S. L is a list whereas S is a Series. Both have values 20,
40,90, 110. What will be the output of the following two statements considering that the
above objects have been created already.

print (L+2) #Statement 1

print (S+2) #Statement 2

ANSWER

Statement 1: print(L+2)

Since L is a list, the + operator is not defined for lists and integers. You will get a
TypeError:

>> TypeError: can only concatenate list (not "int") to list

This is because lists in Python do not support element-wise operations like addition with a
scalar value.

Statement 2: print(S+2)

Since S is a Pandas Series, the + operator is overloaded to perform element-wise addition.


The output will be:

0 22

1 42

2 92

3 112

dtype: int64

This is because the + operator is applied element-wise to each value in the Series, adding 2
to each value.
5. Mr. Harry wants to draw a line chart using a list of elements named LIST. Complete the
code to perform the following operations:

a. To plot a line chart using the given LIST

b. To give a y-axis label to the line chart named "sample number".

import matplotlib.pyplot as PLINE

LIST=[10,20,30,40,50,60]

#Statement 1

#Statement 2

ANSWER

import matplotlib.pyplot as plt

LIST = [10, 20, 30, 40, 50, 60]

# Statement 1: Plot a line chart using the given LIST

plt.plot(LIST)

# Statement 2: Give a y-axis label to the line chart named "sample number"

plt.ylabel("sample number")

# Add a title to the chart (optional)


plt.title("Line Chart Example")

# Show the plot

plt.show()
6. A dictionary ‘stationary’ contains the following:

stationary={‘Name’:[ ‘books’, ‘copies’, ‘pencil box’, ‘Pen’],

‘Price’:[ 500,350, 400,250]

ANSWER

print(stationary['Name'])

['books', 'copies', 'pencil box', 'Pen']

print(stationary['Price'])

[500, 350, 400, 250]

new_dict = dict(zip(stationary['Name'], stationary['Price']))

print(new_dict)

{'books': 500, 'copies': 350, 'pencil box': 400, 'Pen': 250}


7. Write statements for the following:

a. Create a Dataframe named “stock”

b. Add a column called ‘discount’ with the following data: [ 15, 20, 30, 25]

c. Delete column discount with all values.

ANSWER

import pandas as pd

# a. Create a Dataframe named “stock”

stock = pd.DataFrame({

'Name': ['books', 'copies', 'pencil box', 'Pen'],

'Price': [500, 350, 400, 250]

})

# b. Add a column called ‘discount’ with the following data: [ 15, 20, 30, 25]

stock['discount'] = [15, 20, 30, 25]

# c. Delete column discount with all values

stock = stock.drop('discount', axis=1)


8. Differentiate between Single Row and Multi Row Functions?

ANSWER

The main difference between single row and multiple row functions is that single row
functions work on one row at a time and returns one result for every row. While multiple
row functions work on a group of rows and return one result for every group
9. Write a program to create the series that stores the term marks of 10 students. Find the
highest and lowest three marks scored by students.

ANSWER

import pandas as pd

# Create a Series to store the term marks of 10 students

marks = pd.Series([85, 92, 78, 95, 88, 76, 91, 89, 90, 82])

# Print the original Series

print("Original Series:")

print(marks)

# Find the highest three marks

highest_three = marks.nlargest(3)

print("\nHighest three marks:")

print(highest_three)

# Find the lowest three marks

lowest_three = marks.nsmallest(3)

print("\nLowest three marks:")

print(lowest_three)

OUTPUT
10. Write a program to create a series from list marks and taking index values from list roll
no. Show all elements that are above 80 marks.

ANSWER

import pandas as pd

# Create a list of marks

marks = [85, 92, 78, 95, 88, 76, 91, 89, 90, 82]

# Create a list of roll numbers

roll_no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create a Series from the list of marks and roll numbers

series = pd.Series(marks, index=roll_no)

# Print the original Series

print("Original Series:")

print(series)

# Show all elements that are above 80 marks

print("\nElements above 80 marks:")

print(series[series > 80])

88
9

OUTPUT
11. What is the difference between the following two statements, which are creating two
dataframes using the same dataframe (Pandas library has been imported as pd)?

i. Df2=pd.DataFrame(df1) ii. Df3=pd.DataFrame(df1, copy=True)

ANSWER

The difference between the two statements lies in how they handle the data when creating
a new DataFrame.

i. Df2=pd.DataFrame(df1)

When you create a new DataFrame Df2 from an existing DataFrame df1 without specifying
the copy parameter, it creates a new DataFrame object but shares the same data as df1.
This means that both Df2 and df1 point to the same data in memory. Any changes made to
Df2 will also affect df1, and vice versa.

ii. Df3=pd.DataFrame(df1, copy=True)

When you create a new DataFrame Df3 from an existing DataFrame df1 with the
copy=True parameter, it creates a new DataFrame object and copies the data from df1 to
Df3. This means that Df3 has its own separate data in memory, independent of df1. Any
changes made to Df3 will not affect df1, and vice versa
ANSWER

# i) For book and uniform only

print(aid[['Books', 'Uniform']])

# ii)For shoes only

print(aid['Shoes'])
13. What are aggregate functions? How are they useful?

ANSWER

An aggregate function performs a calculation on a set of values, and returns a single value.
Except for COUNT(*) , aggregate functions ignore null values. Aggregate functions are often
used with the GROUP BY clause of the SELECT statement. All aggregate functions are
deterministic

14. What is the use of GROUP BY clause> Give example.

ANSWER

The GROUP BY clause causes the rows of the items table to be collected into groups, each
group composed of rows that have identical order_num values (that is, the items of each
order are grouped together). After the database server forms the groups, the aggregate
functions COUNT and SUM are applied within each group.
15. WAP to plot a line chart between two given arrays

A=[4,56,78,23,45,67,89]

B=[0,1,2,3,4,5,6]

ANSWER

import matplotlib.pyplot as plt

A = [4, 56, 78, 23, 45, 67, 89]

B = [0, 1, 2, 3, 4, 5, 6]

plt.plot(B, A)

plt.xlabel('Index')

plt.ylabel('Value')

plt.title('Line Chart')

plt.show()
ANSWER

import pandas as pd

df1 = pd.DataFrame([

['Arjun', 'XII', 95],

['Ruchi', 'X', 84],

['Prerna', 'XI', 90],

['Himali', 'XI', 75]

], columns=['Name', 'Class', 'Marks'])

print(df1)
ANSWER

import pandas as pd

D1 = pd.Series({

'TEST 1': 30,

'TEST 2': 40,

'TEST 3': 50,

'TEST 4': 100

}, name='MAX MARKS')

print(D1)

18. Consider the decimal number x with value 54567.8577. Write commands in SQL to: i.
display the whole number without round it off. ii.round it to 2 places of the decimal.

ANSWER

SELECT FLOOR(54567.8577) AS WholeNumber;


SELECT ROUND(54567.8577, 2) AS RoundedNumber;

19. A company randomly generated a weekly winning number (between 0. 1000) for 10
weeks. An employee of the company wants to plot the sine values of winningnumbers
(nump.sin( ) function) on a line chart. Write a program to help him accomplish this.

ANSWER

import numpy as np

import matplotlib.pyplot as plt

# Generate 10 random winning numbers between 0 and 1000

winning_numbers = np.random.uniform(0, 1000, 10)

# Calculate the sine values of the winning numbers

sine_values = np.sin(winning_numbers)

# Create a line chart of the sine values

plt.plot(sine_values)

plt.xlabel('Week')

plt.ylabel('Sine Value')

plt.title('Sine Values of Winning Numbers')

plt.show()
20. Write a small python code to create a dataframe with headings (a and b) from the list
given below: [ [1,2],[3,4],[5,6],[7,8] ]

ANSWER

import pandas as pd

data = [[1,2],[3,4],[5,6],[7,8]]

df = pd.DataFrame(data, columns=['a', 'b'])

print(df)

OUTPUT

VEDANT AGGARWAL S7-H

SELF QUERIES
Question 1

Write a query to display EName and Sal of employees whose salary is


greater than or equal to 2200 from table Empl.

Answer

SELECT ENAME, SAL


FROM empl
WHERE SAL >= 2200;

Output

+-----------+------+
| ENAME | SAL |
+-----------+------+
| MAHADEVAN | 2985 |
| BINA | 2850 |
| AMIR | 5000 |
| SHIAVNSH | 2450 |
| SCOTT | 3000 |
| FAKIR | 3000 |
+-----------+------+

Question 2

Write a query to display details of employees who are not getting


commission from table Empl.

Answer

SELECT *
FROM empl
WHERE COMM IS NULL OR COMM = 0;
Output

+-------+-----------+-----------+------+------------+------+------+---
-----+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM |
DEPTNO |
+-------+-----------+-----------+------+------------+------+------+---
-----+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800 | NULL |
20 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985 | NULL |
20 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850 | NULL |
30 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000 | NULL |
10 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500 | 0 |
30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450 | NULL |
10 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100 | NULL |
20 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000 | NULL |
20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950 | NULL |
30 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000 | NULL |
20 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300 | NULL |
10 |
+-------+-----------+-----------+------+------------+------+------+---
-----+

Question 3

Write a query to display employee name and salary of those employee


who don't have their salary in the range of 2500 to 4000.
Answer

SELECT ENAME, SAL


FROM empl
WHERE SAL NOT BETWEEN 2500 AND 4000;

Output

+----------+------+
| ENAME | SAL |
+----------+------+
| SMITH | 800 |
| ANYA | 1600 |
| SETH | 1250 |
| MOMIN | 1250 |
| AMIR | 5000 |
| KULDEEP | 1500 |
| SHIAVNSH | 2450 |
| ANOOP | 1100 |
| JATIN | 950 |
| MITA | 1300 |
+----------+------+

Question 4

Write a query to display the name, job title and salary of employee who
do not have manager.

Answer

SELECT ENAME, JOB, SAL


FROM empl
WHERE MGR IS NULL ;
Output

+-------+-----------+------+
| ENAME | JOB | SAL |
+-------+-----------+------+
| AMIR | PRESIDENT | 5000 |
+-------+-----------+------+

Question 5

Write a query to display the name of employee whose name contains


'A' as third alphabet.

Answer

SELECT ENAME
FROM empl
WHERE ENAME LIKE '__A%' ;

Explanation

There are no employees whose name contains 'A' as the third alphabet
in the empl table. Therefore, the output will be empty.

Question 6

Write a query to display the name of employee whose name contains


'T' as the last alphabet.

Answer

SELECT ENAME
FROM empl
WHERE ENAME LIKE '%T' ;
Output

+-------+
| ENAME |
+-------+
| SCOTT |
+-------+

Question 7

Write a query to display the name of employee whose name contains


'M' as first alphabet 'L' as third alphabet.

Answer

SELECT ENAME
FROM empl
WHERE ENAME LIKE 'M_L%' ;

Explanation

There are no employees whose name contains 'M' as first alphabet and
'L' as third alphabet in the empl table. Therefore, the output will be
empty.

Question 8

Write a query on the customers table whose output will exclude all
customers with a rating <= 100, unless they are located in Shimla.

Answer

SELECT *
FROM customers
WHERE rating > 100 OR city = 'Shimla' ;
Question 9

Write a query that selects all orders (Order table) except those with
zeros or NULLs in the amt field.

Answer

SELECT *
FROM order
WHERE amt IS NOT NULL AND amt <> 0 ;

Question 10

Write SQL commands for the following on the basis of given table
STUDENT :

Table : STUDENT

Student Clas Nam Grad Grad


GAME SUPW
No. s e e1 e2

Same Photogra
10 7 Cricket B A
er phy

Gardenin
11 8 Sujit Tennis A C
g

Swimmi Photogra
12 7 Kamal B B
ng phy

13 7 Veena Tennis C Cooking A

Archa Basket
14 9 A Literature A
na Ball
Gardenin
15 10 Arpit Cricket A C
g
1. Display the names of the students who are getting a grade 'C' in
either GAME or SUPW.
2. Display the different games offered in the school.
3. Display the SUPW taken up by the students, whose name starts
with 'A'.
Answer

1.

SELECT Name
FROM STUDENT
WHERE Grade1 = 'C' OR Grade2 = 'C' ;

Output

+-------+
| Name |
+-------+
| Sujit |
| Veena |
| Arpit |
+-------+

2.

SELECT DISTINCT GAME


FROM STUDENT ;

Output

+-------------+
| GAME |
+-------------+
| Cricket |
| Tennis |
| Swimming |
| Basket Ball |
+-------------+
3.

SELECT SUPW
FROM STUDENT
WHERE Name LIKE 'A%' ;

Output

+------------+
| SUPW |
+------------+
| Literature |
| Gardening |
+------------+

Question 11

Write SQL commands for the following on the basis of given table
SPORTS :

Table : SPORTS

Student Clas Grad Grad


Name Game1 Game2
No. s e1 e2

Same Swimmi
10 7 Cricket B A
er ng

11 8 Sujit Tennis A Skating C


Swimmi
12 7 Kamal B Football B
ng

13 7 Venna Tennis C Tennis A

Archa Basketb
14 9 A Cricket A
na all

Athletic
15 10 Arpit Cricket A C
s
4. Display the names of the students who have grade 'C' in either
Game1 or Game2 or both.
5. Display the names of the students who have same game for both
Game1 and Game2.
6. Display the games taken up by the students, whose name starts
with 'A'.
Answer

1.

SELECT Name
FROM SPORTS
WHERE Grade1 = 'C' OR Grade2 = 'C' ;

Output

+-------+
| Name |
+-------+
| Sujit |
| Venna |
| Arpit |
+-------+

2.
SELECT Name
FROM SPORTS
WHERE Game1 = Game2 ;

Output

+-------+
| Name |
+-------+
| Venna |
+-------+

3.

SELECT Game1, Game2


FROM SPORTS
WHERE Name LIKE 'A%' ;

Output

+------------+-----------+
| Game1 | Game2 |
+------------+-----------+
| Basketball | Cricket |
| Cricket | Athletics |
+------------+-----------+

Question 12

Write SQL commands for the following on the basis of given table
CLUB :

Table : CLUB

COACH_ COACHNA AG PA SE DATOFA


SPORTS
ID ME E Y X PP
10 1996-03-
1 KUKREJA 35 KARATE M
00 27

12 1998-01-
2 RAVINA 34 KARATE F
00 20

20 1998-02-
3 KARAN 34 SQUASH M
00 19

BASKETBA 15 1998-01-
4 TARUN 33 M
LL 00 01

SWIMMIN 75 1998-01-
5 ZUBIN 36 M
G 0 12

SWIMMIN 80 1998-02-
6 KETAKI 36 F
G 0 24

22 1998-02-
7 ANKITA 39 SQUASH F
00 20

11 1998-02-
8 ZAREEN 37 KARATE F
00 22

SWIMMIN 90 1998-01-
9 KUSH 41 M
G 0 13

BASKETBA 17 1998-02-
10 SHAILYA 37 M
LL 00 19
7. To show all information about the swimming coaches in the club.
8. To list names of all coaches with their date of appointment
(DATOFAPP) in descending order.
9. To display a report, showing coachname, pay, age and bonus (15%
of pay) for all the coaches.
Answer

1.

SELECT *
FROM CLUB
WHERE SPORTS = 'SWIMMING' ;

Output

+----------+-----------+-----+----------+-----+-----+------------+
| COACH_ID | COACHNAME | AGE | SPORTS | PAY | SEX | DATOFAPP |
+----------+-----------+-----+----------+-----+-----+------------+
| 5 | ZUBIN | 36 | SWIMMING | 750 | M | 1998-01-12 |
| 6 | KETAKI | 36 | SWIMMING | 800 | F | 1998-02-24 |
| 9 | KUSH | 41 | SWIMMING | 900 | M | 1998-01-13 |
+----------+-----------+-----+----------+-----+-----+------------+
2.
SELECT COACHNAME, DATOFAPP
FROM CLUB
ORDER BY DATOFAPP DESC ;

Output

+-----------+------------+
| COACHNAME | DATOFAPP |
+-----------+------------+
| KETAKI | 1998-02-24 |
| ZAREEN | 1998-02-22 |
| ANKITA | 1998-02-20 |
| KARAN | 1998-02-19 |
| SHAILYA | 1998-02-19 |
| RAVINA | 1998-01-20 |
| KUSH | 1998-01-13 |
| ZUBIN | 1998-01-12 |
| TARUN | 1998-01-01 |
| KUKREJA | 1996-03-27 |
+-----------+------------+

3.

SELECT COACHNAME, PAY, AGE, (PAY * 0.15) AS BONUS


FROM CLUB ;

Output

+-----------+------+-----+--------+
| COACHNAME | PAY | AGE | BONUS |
+-----------+------+-----+--------+
| KUKREJA | 1000 | 35 | 150.00 |
| RAVINA | 1200 | 34 | 180.00 |
| KARAN | 2000 | 34 | 300.00 |
| TARUN | 1500 | 33 | 225.00 |
| ZUBIN | 750 | 36 | 112.50 |
| KETAKI | 800 | 36 | 120.00 |
| ANKITA | 2200 | 39 | 330.00 |
| ZAREEN | 1100 | 37 | 165.00 |
| KUSH | 900 | 41 | 135.00 |
| SHAILYA | 1700 | 37 | 255.00 |
+-----------+------+-----+--------+

Question 13

Write SQL commands for the following on the basis of given table
STUDENT1 :

Table : STUDENT1

No Stipen AvgMar Grad


Name Stream Class
. d k e
1 Karan 400.00 Medical 78.5 B 12B

Divaka
2 450.00 Commerce 89.2 A 11C
r

3 Divya 300.00 Commerce 68.6 C 12C

Humanitie
4 Arun 350.00 73.1 B 12C
s

Nonmedic
5 Sabina 500.00 90.6 A 11A
al

6 John 400.00 Medical 75.4 B 12B

Humanitie
7 Robert 250.00 64.4 C 11A
s

Nonmedic
8 Rubina 450.00 88.5 A 12A
al

Nonmedic
9 Vikas 500.00 92.0 A 12A
al

10 Mohan 300.00 Commerce 67.5 C 12C


10. Select all the Nonmedical stream students from STUDENT1.
11. List the names of those students who are in class 12 sorted
by Stipend.
12. List all students sorted by AvgMark in descending order.
13. Display a report, listing Name, Stipend, Stream and amount
of stipend received in a year assuming that the Stipend is paid
every month.
Answer
1.

SELECT *
FROM STUDENT1
WHERE Stream = 'Nonmedical' ;

Output

+-----+--------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+--------+---------+------------+---------+-------+-------+
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
+-----+--------+---------+------------+---------+-------+-------+

2.

SELECT Name
FROM STUDENT1
WHERE Class LIKE '12%'
ORDER BY Stipend ;

Output

+--------+
| Name |
+--------+
| Divya |
| Mohan |
| Arun |
| Karan |
| John |
| Rubina |
| Vikas |
+--------+
3.

SELECT *
FROM STUDENT1
ORDER BY AvgMark DESC ;

Output

+-----+---------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+---------+---------+------------+---------+-------+-------+
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 2 | Divakar | 450 | Commerce | 89.2 | A | 11C |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 1 | Karan | 400 | Medical | 78.5 | B | 12B |
| 6 | John | 400 | Medical | 75.4 | B | 12B |
| 4 | Arun | 350 | Humanities | 73.1 | B | 12C |
| 3 | Divya | 300 | Commerce | 68.6 | C | 12C |
| 10 | Mohan | 300 | Commerce | 67.5 | C | 12C |
| 7 | Robert | 250 | Humanities | 64.4 | C | 11A |
+-----+---------+---------+------------+---------+-------+-------+

4.

SELECT Name, Stipend, Stream, (Stipend * 12) AS Yearly_Stipend


FROM STUDENT1 ;

Output

+---------+---------+------------+----------------+
| Name | Stipend | Stream | Yearly_Stipend |
+---------+---------+------------+----------------+
| Karan | 400 | Medical | 4800 |
| Divakar | 450 | Commerce | 5400 |
| Divya | 300 | Commerce | 3600 |
| Arun | 350 | Humanities | 4200 |
| Sabina | 500 | Nonmedical | 6000 |
| John | 400 | Medical | 4800 |
| Robert | 250 | Humanities | 3000 |
| Rubina | 450 | Nonmedical | 5400 |
| Vikas | 500 | Nonmedical | 6000 |
| Mohan | 300 | Commerce | 3600 |
+---------+---------+------------+----------------+

Question 14

Consider the table Student1 of Q. 13. Give the output of following SQL
statement :

14. SELECT TRUNCATE(AvgMark) FROM Student1 WHERE


AvgMark < 75 ;
15. SELECT ROUND(AvgMark) FROM Student1 WHERE Grade =
'B' ;
16. SELECT CONCAT(Name, Stream) FROM Student1 WHERE
Class = '12A' ;
17. SELECT RIGHT(Stream, 2) FROM Student1 ;
Answer

1. It will return error because no argument is passed as decimal places


to truncate. Syntax of truncate function is TRUNCATE(number, decimals).

2.

Output

+----------------+
| ROUND(AvgMark) |
+----------------+
| 78 |
| 73 |
| 75 |
+----------------+
3.

Output

+----------------------+
| CONCAT(Name, Stream) |
+----------------------+
| RubinaNonmedical |
| VikasNonmedical |
+----------------------+
4.

Output

+------------------+
| RIGHT(Stream, 2) |
+------------------+
| al |
| ce |
| ce |
| es |
| al |
| al |
| es |
| al |
| al |
| ce |
+------------------+

Question 15

Given the following table :

Table : STUDENT
No Stipen AvgMar Grad
Name Stream Class
. d k e

1 Karan 400.00 Medical 78.5 B 12B

Divaka
2 450.00 Commerce 89.2 A 11C
r

3 Divya 300.00 Commerce 68.6 C 12C

Humanitie
4 Arun 350.00 73.1 B 12C
s

Nonmedic
5 Sabina 500.00 90.6 A 11A
al

6 John 400.00 Medical 75.4 B 12B

Humanitie
7 Robert 250.00 64.4 C 11A
s

Nonmedic
8 Rubina 450.00 88.5 A 12A
al

Nonmedic
9 Vikas 500.00 92.0 A 12A
al

10 Mohan 300.00 Commerce 67.5 C 12C


Give the output of following SQL statements :

18. SELECT MIN(AvgMark) FROM STUDENT WHERE AvgMark <


75 ;
19. SELECT SUM(Stipend) FROM STUDENT WHERE Grade = 'B' ;
20. SELECT AVG(Stipend) FROM STUDENT WHERE Class = '12A' ;
21. SELECT COUNT(DISTINCT) FROM STUDENT ;
Answer

1.

Output

+--------------+
| MIN(AvgMark) |
+--------------+
| 64.4 |
+--------------+
2.

Output

+--------------+
| SUM(Stipend) |
+--------------+
| 1150 |
+--------------+
3.

Output

+--------------+
| AVG(Stipend) |
+--------------+
| 475 |
+--------------+

4. It will give an error because the COUNT function requires an argument


specifying what to count. Additionally, the DISTINCT keyword is followed
by a column name to count the distinct values of that column.
Question 16

Write SQL commands for the following on the basis of given table MOV

Table : MOV

N Ratin Qt Pric
Title Type Stars
o g y e

Gone with the 39.9


1 Drama G Gable 4
Wind 5

69.9
2 Friday the 13th Horror R Jason 2
5

49.9
3 Top Gun Drama PG Cruise 7
5

Comed 29.9
4 Splash PG13 Hanks 3
y 5

Independence 19.9
5 Drama R Turner 3
Day 5

Comed 44.9
6 Risky Business R Cruise 2
y 5

Amech 31.9
7 Cocoon Scifi PG 2
e 5

Comed 69.9
8 Crocodile Dundee PG13 Harris 2
y 5

Comed 59.9
9 101 Dalmatians G 3
y 5
Comed Hoffma 29.9
10 Tootsie PG 1
y n 5
22. Display a list of all movies with Price over 20 and sorted by
Price.
23. Display all the movies sorted by QTY in decreasing order.
24. Display a report listing a movie number, current value and
replacement value for each movie in the above table. Calculate
the replacement value for all movies as : QTY * Price * 1.15.
Answer

1.

SELECT Title
FROM MOV
WHERE Price > 20
ORDER BY Price ;

Output

+--------------------+
| Title |
+--------------------+
| Splash |
| Tootsie |
| Cocoon |
| Gone with the Wind |
| Risky Business |
| Top Gun |
| 101 Dalmatians |
| Friday the 13th |
| Crocodile Dundee |
+--------------------+

2.
SELECT Title
FROM MOV
ORDER BY Qty DESC ;

Output

+--------------------+
| Title |
+--------------------+
| Top Gun |
| Gone with the Wind |
| Splash |
| Independence Day |
| 101 Dalmatians |
| Friday the 13th |
| Risky Business |
| Cocoon |
| Crocodile Dundee |
| Tootsie |
+--------------------+

3.

SELECT No AS Movie_Number , Price AS Current_Value, (Qty * Price *


1.15) AS Replacement_Value
FROM MOV ;

Output

+--------------+---------------+--------------------+
| Movie_Number | Current_Value | Replacement_Value |
+--------------+---------------+--------------------+
| 1 | 39.95 | 183.77000350952147 |
| 2 | 69.95 | 160.884992980957 |
| 3 | 49.95 | 402.09750614166256 |
| 4 | 29.95 | 103.3275026321411 |
| 5 | 19.95 | 68.8275026321411 |
| 6 | 44.95 | 103.38500175476074 |
| 7 | 31.95 | 73.48500175476073 |
| 8 | 69.95 | 160.884992980957 |
| 9 | 59.95 | 206.8275026321411 |
| 10 | 29.95 | 34.44250087738037 |
+--------------+---------------+--------------------+

INDIRA GANDHI
VS
SHRI RAJ NARAIN
MC MEHTA
VS
UNION OF INDIA
JESSICA LAL
MURDER CASE
RAJAGOPAL
VS
STATE OF TAMIL NADU

You might also like