IP Assignment
IP Assignment
IP Assignment
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 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, 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>
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 CONCAT(Name, Age) FROM HOSPITAL WHERE Sex='F' AND Age
< 18; -- (viii)
+-------------------+
| CONCAT(Name, Age) |
+-------------------+
| Ketaki16 |
+-------------------+
1 row in set (0.00 sec)