IP Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Assignment 1

CREATE TABLE biodata (


-> NAME VARCHAR (50),
-> ADDRESS VARCHAR (100),
-> TELENO VARCHAR (15),
-> DOB DATE,
-> COMM DECIMAL (8,2)
-> );
Query OK, 0 rows affected (0.03 sec)

MySQL> -- Inserting values into the biodata table


MySQL> INSERT INTO biodata (NAME, ADDRESS, TELENO, DOB, COMM) VALUES
-> ('Aman verma', '123 PUNJABI BAGH', '9899125688', '1992-03-12', 765.89),
-> ('Shreekant singh', '45, VASUNDHARA APPT', '9899675322', '1995-04-18',
123.45),
-> ('Samaira jain', '34 LAKE VIEW APTT', '9856781324', '1998-09-21', 879.12),
-> ('Rohan walia', '13/7 PASCHIM VIHAR', '9935267888', '1996-02-23', 657.76);
Query OK, 4 rows affected (0.02 sec)

MySQL> -- (a) To display name in uppercase and address in lowercase


MySQL> SELECT UPPER(NAME), LOWER(ADDRESS) FROM biodata;
+-----------------+---------------------+
| UPPER(NAME) | LOWER(ADDRESS) |
+-----------------+---------------------+
| AMAN VERMA | 123 punjabi bagh |
| SHREEKANT SINGH | 45, vasundhara appt |
| SAMAIRA JAIN | 34 lake view aptt |
| ROHAN WALIA | 13/7 paschim vihar |
+-----------------+---------------------+
4 rows in set (0.00 sec)
MySQL> -- (b) xxx

MySQL>
MySQL> -- (c) To display the last five characters of address
MySQL> SELECT RIGHT(ADDRESS, 5) FROM biodata;
+-------------------+
| RIGHT(ADDRESS, 5) |
+-------------------+
| BAGH |
| APPT |
| APTT |
| VIHAR |
+-------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (d) To display the first two characters of name in uppercase
MySQL> SELECT UPPER(MID(NAME, 2)) FROM biodata;
+------------------------------+
| UPPER(MID(NAME, 2)) |
+------------------------------+
| AM |
| SH |
| SA |
| RO |
+------------------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (e) To display the number of characters in name
MySQL> SELECT LENGTH(NAME) FROM biodata;
+--------------+
| LENGTH(NAME) |
+--------------+
| 10 |
| 15 |
| 12 |
| 11 |
+--------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (f) To display the address from 4th position to 10th position
MySQL> SELECT MID(ADDRESS, 4, 7) FROM biodata;
+--------------------------+
| MID(ADDRESS, 4, 7) |
+--------------------------+
| PUNJAB |
| VASUND |
| LAKE VI |
| 7 PASCH |
+--------------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (g) To display the name from 2nd position till last character
MySQL> SELECT MID(NAME, 2) FROM biodata;
+--------------------+
| MID(NAME, 2) |
+--------------------+
| man verma |
| hreekant singh |
| amaira jain |
| ohan walia |
+--------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (h) To display the approximate age in years (by subtracting date of
birth from today's date)
MySQL> SELECT TIMESTAMPDIFF(YEAR, DOB, CURDATE()) AS Age FROM biodata;
+------+
| Age |
+------+
| 32 |
| 29 |
| 25 |
| 28 |
+------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (i) To display the month of date of birth
MySQL> SELECT MONTH(DOB) FROM biodata;
+------------+
| MONTH(DOB) |
+------------+
| 3|
| 4|
| 9|
| 2|
+------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (j) To display the year of date of birth
MySQL> SELECT YEAR(DOB) FROM biodata;
+-----------+
| YEAR(DOB) |
+-----------+
| 1992 |
| 1995 |
| 1998 |
| 1996 |
+-----------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (k) To display the day name of date of birth
MySQL> SELECT DAYNAME(DOB) FROM biodata;

+--------------+
| DAYNAME(DOB) |
+--------------+
| Thursday |
| Tuesday |
| Monday |
| Friday |
+--------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (l) To display the day of month of date of birth
MySQL> SELECT DAY(DOB) FROM biodata;
+----------+
| DAY(DOB) |
+----------+
| 12 |
| 18 |
| 21 |
| 23 |
+----------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (m) To display the day of week of date of birth
MySQL> SELECT DAYOFWEEK(DOB) FROM biodata;
+----------------+
| DAYOFWEEK(DOB) |
+----------------+
| 5|
| 3|
| 2|
| 6|
+----------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (n) To display the day of year of date of birth
MySQL> SELECT DAYOFYEAR(DOB) FROM biodata;
+----------------+
| DAYOFYEAR(DOB) |
+----------------+
| 72 |
| 108 |
| 264 |
| 54 |
+----------------+
4 rows in set (0.00 sec)
MySQL>
MySQL> -- (o) To display the commission truncated to -2, -1, 0, and 1 decimal
places
MySQL> SELECT TRUNCATE(COMM, -2), TRUNCATE(COMM, -1),
TRUNCATE(COMM, 0), TRUNCATE(COMM, 1) FROM biodata;
+--------------------+--------------------+-------------------+-------------------+
| TRUNCATE(COMM, -2) | TRUNCATE(COMM, -1) | TRUNCATE(COMM, 0) |
TRUNCATE(COMM, 1) |
+--------------------+--------------------+-------------------+-------------------+
| 700 | 760 | 765 | 765.8 |
| 100 | 120 | 123 | 123.4 |
| 800 | 870 | 879 | 879.1 |
| 600 | 650 | 657 | 657.7 |
+--------------------+--------------------+-------------------+-------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (p) To display the commission rounded to -2, -1, 0, and 1 decimal
places
MySQL> SELECT ROUND(COMM, -2), ROUND(COMM, -1), ROUND(COMM, 0),
ROUND(COMM, 1) FROM biodata;
+-----------------+-----------------+----------------+----------------+
| ROUND(COMM, -2) | ROUND(COMM, -1) | ROUND(COMM, 0) | ROUND(COMM, 1) |
+-----------------+-----------------+----------------+----------------+
| 800 | 770 | 766 | 765.9 |
| 100 | 120 | 123 | 123.5 |
| 900 | 880 | 879 | 879.1 |
| 700 | 660 | 658 | 657.8 |
+-----------------+-----------------+----------------+----------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (q) To display commission truncated to an integer value
MySQL> SELECT TRUNCATE(COMM, 0) FROM biodata;
+-------------------+
| TRUNCATE(COMM, 0) |
+-------------------+
| 765 |
| 123 |
| 879 |
| 657 |
+-------------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (r) To display commission rounded to an integer value
MySQL> SELECT ROUND(COMM) FROM biodata;
+-------------+
| ROUND(COMM) |
+-------------+
| 766 |
| 123 |
| 879 |
| 658 |
+-------------+
4 rows in set (0.00 sec)

MySQL>
MySQL> -- (s) Your half yearly exams will start after 15 days. Find out the name
of the day (dayname) where it falls.
MySQL> -- (If it falls on the next month, null will be displayed)
MySQL> SELECT DAYNAME(DATE_ADD(CURDATE(), INTERVAL 15 DAY)) AS
Exam_Day;
+----------+
| Exam_Day |
+----------+
| Friday |
+----------+
1 row in set (0.00 sec)

Hospital Table

mysql> SELECT * FROM HOSPITAL WHERE Department = 'Cardiology'; -- (a)


+------+--------+------+------------+------------+---------+------+
| PNo | Name | Age | Department | DateofAdm | Charges | Sex |
+------+--------+------+------------+------------+---------+------+
| 7 | Ankita | 29 | Cardiology | 1998-02-20 | 800 | F |
| 9 | Kush | 19 | Cardiology | 1998-01-13 | 800 | M |
+------+--------+------+------------+------------+---------+------+
2 rows in set (0.00 sec)

mysql> SELECT Name FROM HOSPITAL WHERE Sex = 'F' AND (Department =
'Orthopedic' OR Department = 'Surgery'); -- (b)
+--------+
| Name |
+--------+
| Ravina |
+--------+
1 row in set (0.00 sec)

mysql> SELECT Name, DateofAdm FROM HOSPITAL ORDER BY DateofAdm; -- (c)


+---------+------------+
| Name | DateofAdm |
+---------+------------+
| Ravina | 1998-01-01 |
| Tarun | 1998-01-01 |
| Zubin | 1998-01-12 |
| Kush | 1998-01-13 |
| Karan | 1998-02-19 |
| Snailya | 1998-02-19 |
| Ankita | 1998-02-20 |
| Zareena | 1998-02-22 |
| Sandeep | 1998-02-23 |
| Ketaki | 1998-02-24 |
+---------+------------+
10 rows in set (0.00 sec)

mysql> SELECT Name, Charges, Age FROM HOSPITAL WHERE Sex = 'M'; -- (d)
+---------+---------+------+
| Name | Charges | Age |
+---------+---------+------+
| Sandeep | 300 | 65 |
| Karan | 200 | 45 |
| Tarun | 300 | 12 |
| Zubin | 250 | 36 |
| Kush | 800 | 19 |
| Snailya | 400 | 31 |
+---------+---------+------+
6 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM HOSPITAL WHERE Age > 20; -- (e)
+----------+
| COUNT(*) |
+----------+
| 7|
+----------+
1 row in set (0.00 sec)

mysql> SELECT DISTINCT Department FROM HOSPITAL; -- (f)


+------------+
| Department |
+------------+
| Surgery |
| Orthopedic |
| ENT |
| Cardiology |
| Gynecology |
| Medicine |
+------------+
6 rows in set (0.00 sec)

mysql> SELECT Department, COUNT(*) AS Num_Patients FROM HOSPITAL GROUP


BY Department; -- (g)
+------------+--------------+
| Department | Num_Patients |
+------------+--------------+
| Surgery | 2|
| Orthopedic | 2|
| ENT | 2|
| Cardiology | 2|
| Gynecology | 1|
| Medicine | 1|
+------------+--------------+
6 rows in set (0.00 sec)

mysql> SELECT Sex, COUNT(*) AS Num_Patients FROM HOSPITAL GROUP BY Sex;


-- (h)
+------+--------------+
| Sex | Num_Patients |
+------+--------------+
|M | 6|
|F | 4|
+------+--------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM HOSPITAL WHERE DateofAdm >= '1998-01-01' AND


DateofAdm <= '1998-03-31'; -- (i)
+------+---------+------+------------+------------+---------+------+
| PNo | Name | Age | Department | DateofAdm | Charges | Sex |
+------+---------+------+------------+------------+---------+------+
| 1 | Sandeep | 65 | Surgery | 1998-02-23 | 300 | M |
| 2 | Ravina | 24 | Orthopedic | 1998-01-01 | 200 | F |
| 3 | Karan | 45 | Orthopedic | 1998-02-19 | 200 | M |
| 4 | Tarun | 12 | Surgery | 1998-01-01 | 300 | M |
| 5 | Zubin | 36 | ENT | 1998-01-12 | 250 | M |
| 6 | Ketaki | 16 | ENT | 1998-02-24 | 300 | F |
| 7 | Ankita | 29 | Cardiology | 1998-02-20 | 800 | F |
| 8 | Zareena | 45 | Gynecology | 1998-02-22 | 300 | F |
| 9 | Kush | 19 | Cardiology | 1998-01-13 | 800 | M |
| 10 | Snailya | 31 | Medicine | 1998-02-19 | 400 | M |
+------+---------+------+------------+------------+---------+------+
10 rows in set (0.00 sec)

mysql> SELECT Department FROM HOSPITAL GROUP BY Department HAVING


COUNT(*) < 2; -- (j)
+------------+
| Department |
+------------+
| Gynecology |
| Medicine |
+------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM HOSPITAL WHERE Name LIKE 'Z%'; -- (k)


+------+---------+------+------------+------------+---------+------+
| PNo | Name | Age | Department | DateofAdm | Charges | Sex |
+------+---------+------+------------+------------+---------+------+
| 5 | Zubin | 36 | ENT | 1998-01-12 | 250 | M |
| 8 | Zareena | 45 | Gynecology | 1998-02-22 | 300 | F |
+------+---------+------+------------+------------+---------+------+
2 rows in set (0.00 sec)

mysql> UPDATE HOSPITAL SET Age = 20 WHERE Name = 'Kush'; -- (l)


Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> UPDATE HOSPITAL SET Charges = Charges * 1.05; -- (m)


Query OK, 10 rows affected (0.00 sec)
Rows matched: 10 Changed: 10 Warnings: 0

mysql> DELETE FROM HOSPITAL WHERE Name = 'Tarun'; -- (n)


Query OK, 1 row affected (0.00 sec)

mysql> ALTER TABLE HOSPITAL ADD WardNumber INT; -- (o)


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> UPDATE HOSPITAL SET Charges = NULL WHERE Department = 'Surgery';


-- (q)
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> UPDATE HOSPITAL SET Charges = Charges * 0.9 WHERE Department =


'Cardiology'; -- (r)
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> INSERT INTO HOSPITAL (PNo, Name, Age, Department, DateofAdm,


Charges, Sex) VALUES (11, 'Mustafa', 37, 'ENT', '1998-02-25', 250, 'M'); -- (t)
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> -- Outputs of the given SQL statements:
mysql> SELECT COUNT(DISTINCT Charges) FROM HOSPITAL; -- (i)
+-------------------------+
| COUNT(DISTINCT Charges) |
+-------------------------+
| 6|
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT MIN(Age) FROM HOSPITAL WHERE Sex='M'; -- (ii)
+----------+
| MIN(Age) |
+----------+
| 20 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT AVG(Charges) FROM HOSPITAL WHERE DateofAdm < '1998-02-


12'; -- (iii)
+--------------+
| AVG(Charges) |
+--------------+
| 409.6667 |
+--------------+
1 row in set (0.00 sec)

mysql> SELECT SUM(Charges) FROM HOSPITAL WHERE Sex ='F'; -- (iv)


+--------------+
| SUM(Charges) |
+--------------+
| 1596 |
+--------------+
1 row in set (0.00 sec)

mysql> SELECT LEFT(Name, 4) FROM HOSPITAL WHERE Charges = 200; -- (v)


Empty set (0.00 sec)

mysql> SELECT RIGHT(Name, 3) FROM HOSPITAL WHERE Department='ENT'; --


(vi)
+----------------+
| RIGHT(Name, 3) |
+----------------+
| bin |
| aki |
| afa |
+----------------+
3 rows in set (0.00 sec)

mysql> SELECT LENGTH(Department) FROM HOSPITAL WHERE Department IN


('ENT', 'Orthopedic'); -- (vii)
+--------------------+
| LENGTH(Department) |
+--------------------+
| 10 |
| 10 |
| 3|
| 3|
| 3|
+--------------------+
5 rows in set (0.00 sec)

mysql> SELECT CONCAT(Name, Age) FROM HOSPITAL WHERE Sex='F' AND Age
< 18; -- (viii)
+-------------------+
| CONCAT(Name, Age) |
+-------------------+
| Ketaki16 |
+-------------------+
1 row in set (0.00 sec)

mysql> SELECT LCASE(Department) FROM HOSPITAL WHERE Department LIKE


'%y'; -- (ix)
+-------------------+
| LCASE(Department) |
+-------------------+
| surgery |
| cardiology |
| gynecology |
| cardiology |
+-------------------+
4 rows in set (0.00 sec)

mysql> SELECT SUBSTR(Department, 2, 3) FROM HOSPITAL WHERE Charges


BETWEEN 500 AND 1000; -- (x)
+--------------------------+
| SUBSTR(Department, 2, 3) |
+--------------------------+
| ard |
| ard |
+--------------------------+
2 rows in set (0.00 sec)

mysql> SELECT MONTH(DateofAdm), DAY(DateofAdm) FROM HOSPITAL WHERE


Charges BETWEEN 500 AND 1000; -- (xi)
+------------------+----------------+
| MONTH(DateofAdm) | DAY(DateofAdm) |
+------------------+----------------+
| 2| 20 |
| 1| 13 |
+------------------+----------------+
2 rows in set (0.00 sec)

mysql> SELECT CURDATE(), DAYNAME(CURDATE()); -- (xii)


+------------+--------------------+
| CURDATE() | DAYNAME(CURDATE()) |
+------------+--------------------+
| 2024-05-07 | Tuesday |
+------------+--------------------+
1 row in set (0.00 sec)

mysql> SELECT DAYOFWEEK(CURDATE()), DAYOFYEAR(CURDATE()); -- (xiii)


+----------------------+----------------------+
| DAYOFWEEK(CURDATE()) | DAYOFYEAR(CURDATE()) |
+----------------------+----------------------+
| 3| 128 |
+----------------------+----------------------+
1 row in set (0.00 sec)

mysql> SELECT ROUND(2345.67,2), ROUND(2345.67,1), ROUND(2345.67,0); --


(xiv)
+------------------+------------------+------------------+
| ROUND(2345.67,2) | ROUND(2345.67,1) | ROUND(2345.67,0) |
+------------------+------------------+------------------+
| 2345.67 | 2345.7 | 2346 |
+------------------+------------------+------------------+
1 row in set (0.00 sec)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy