DBMS Lab File Works 2
DBMS Lab File Works 2
Write SQL queries using character, number, date, and group functions.
+------+-----------------+-----+--------+-----------------+----------+
| id | name | age | sub_id | date_of_joining | salary |
+------+-----------------+-----+--------+-----------------+----------+
| 1001 | Sharda Tiwari | 27 | 1 | 2020-03-20 | 70000.00 |
| 1002 | Shashwat Shukla | 36 | 2 | 2024-05-11 | 35000.00 |
| 1003 | Ritu Dwivedi | 29 | 3 | 2023-02-03 | 68000.00 |
| 1004 | Chetna Singh | 39 | 4 | 2025-02-10 | 65000.00 |
| 1005 | Rakesh Sharma | 40 | 5 | 2023-10-15 | 40000.00 |
| 1006 | Pooja Verma | 28 | NULL | 2022-04-01 | 62000.00 |
| 1007 | Suman Sharma | 33 | 1 | 2021-05-05 | 68000.00 |
| 1008 | Avinash Pandey | 27 | NULL | 2023-09-06 | 60000.00 |
| 1009 | Sonam Singh | 24 | NULL | 2020-08-09 | 65000.00 |
| 1010 | Faraz Khan | 35 | NULL | 2022-03-19 | 55000.00 |
| 1011 | Arunima | 24 | NULL | 2021-11-27 | 63000.00 |
+------+-----------------+-----+--------+-----------------+----------+
(1) SQL Queries Using Character (2) SQL Queries Using Number
Functions Functions
15
mysql> SELECT name, LENGTH(name) AS (3) SQL Queries Using Date
name_length FROM tbl_teachers LIMIT 5;
+-----------------+-------------+ Functions
| name | name_length |
+-----------------+-------------+ mysql> SELECT NOW() AS curr_timestamp;
| Sharda Tiwari | 13 | +---------------------+
| Shashwat Shukla | 15 | | curr_timestamp |
| Ritu Dwivedi | 12 | +---------------------+
| Chetna Singh | 12 | | 2025-03-06 11:13:39 |
| Rakesh Sharma | 13 | +---------------------+
+-----------------+-------------+ 1 row in set (0.00 sec)
5 rows in set (0.00 sec)
mysql> SELECT CURDATE() AS today_date;
mysql> SELECT name AS before_concat, +------------+
CONCAT(‘Prof. ‘, name) AS after_concat | today_date |
FROM tbl_teachers LIMIT 6 OFFSET 5; +------------+
+----------------+----------------------+ | 2025-03-06 |
| before_concat | after_concat | +------------+
+----------------+----------------------+ 1 row in set (0.00 sec)
| Pooja Verma | Prof. Pooja Verma |
| Suman Sharma | Prof. Suman Sharma | mysql> SELECT DAY(CURDATE()) AS day,
| Avinash Pandey | Prof. Avinash Pandey | MONTH(CURDATE()) AS month,
| Sonam Singh | Prof. Sonam Singh | YEAR(CURDATE()) as year;
| Faraz Khan | Prof. Faraz Khan | +-----+-------+------+
| Arunima | Prof. Arunima | | day | month | year |
+----------------+----------------------+ +-----+-------+------+
6 rows in set (0.00 sec) | 6 | 3 | 2025 |
+-----+-------+------+
mysql> SELECT name AS full_name, 1 row in set (0.00 sec)
SUBSTRING(name, 1, LOCATE(' ', name) - 1)
AS first_name FROM tbl_teachers LIMIT 5; mysql> SELECT HOUR(NOW()) AS hour,
+-----------------+------------+ MINUTE(NOW()) AS minute,
| full_name | first_name | SECOND(NOW()) AS second;
+-----------------+------------+ +------+--------+--------+
| Sharda Tiwari | Sharda | | hour | minute | second |
| Shashwat Shukla | Shashwat | +------+--------+--------+
| Ritu Dwivedi | Ritu | | 11 | 16 | 50 |
| Chetna Singh | Chetna | +------+--------+--------+
| Rakesh Sharma | Rakesh | 1 row in set (0.00 sec)
+-----------------+------------+
5 rows in set (0.00 sec)
mysql> SELECT
-> COUNT(*) AS total_teachers,
-> MAX(salary) AS max_salary_amt,
-> MIN(salary) AS min_salary_amt,
-> AVG(salary) AS average_of_salary
-> FROM tbl_teachers;
16
+----------------+----------------+----------------+-------------------+
| total_teachers | max_salary_amt | min_salary_amt | average_of_salary |
+----------------+----------------+----------------+-------------------+
| 11 | 70000.00 | 35000.00 | 59181.818182 |
+----------------+----------------+----------------+-------------------+
1 row in set (0.00 sec)
Program 5
Write SQL queries for extracting data from more than one table.
👉 Data from more than one table can be extracted using the JOIN functions. The
JOIN function are of 4 types – INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
👉 FULL JOIN – extracts all data ( left + right ), but not available in MySQL.
+------+-----------------+-----+--------+-----------------+----------+
| id | name | age | sub_id | date_of_joining | salary |
+------+-----------------+-----+--------+-----------------+----------+
| 1001 | Sharda Tiwari | 27 | 1 | 2020-03-20 | 70000.00 |
| 1002 | Shashwat Shukla | 36 | 2 | 2024-05-11 | 35000.00 |
| 1003 | Ritu Dwivedi | 29 | 3 | 2023-02-03 | 68000.00 |
| 1004 | Chetna Singh | 39 | 4 | 2025-02-10 | 65000.00 |
| 1005 | Rakesh Sharma | 40 | 5 | 2023-10-15 | 40000.00 |
| 1006 | Pooja Verma | 28 | NULL | 2022-04-01 | 62000.00 |
| 1007 | Suman Sharma | 33 | 1 | 2021-05-05 | 68000.00 |
| 1008 | Avinash Pandey | 27 | NULL | 2023-09-06 | 60000.00 |
| 1009 | Sonam Singh | 24 | NULL | 2020-08-09 | 65000.00 |
| 1010 | Faraz Khan | 35 | NULL | 2022-03-19 | 55000.00 |
| 1011 | Arunima | 24 | NULL | 2021-11-27 | 63000.00 |
+------+-----------------+-----+--------+-----------------+----------+
+--------+------+
| sub_id | sub |
+--------+------+
| 1 | DBMS |
| 2 | OS |
| 3 | SE |
| 4 | SNT |
| 5 | COA |
+--------+------+
17
(1) Extracting Data Using INNER JOIN
18
Program 6
👉 Sub-queries are the queries nested inside another (big) query. They are used to
fetch complex data demands by performing operations like filtering, aggregations,
and comparisons before the main query is executed. Sub-queries are of 3 types –
Single Row Sub-query, Multiple Row Sub-query, and Correlated Sub-query.
+------+-----------------+-----+--------+-----------------+----------+
| id | name | age | sub_id | date_of_joining | salary |
+------+-----------------+-----+--------+-----------------+----------+
| 1001 | Sharda Tiwari | 27 | 1 | 2020-03-20 | 70000.00 |
| 1002 | Shashwat Shukla | 36 | 2 | 2024-05-11 | 35000.00 |
| 1003 | Ritu Dwivedi | 29 | 3 | 2023-02-03 | 68000.00 |
| 1004 | Chetna Singh | 39 | 4 | 2025-02-10 | 65000.00 |
| 1005 | Rakesh Sharma | 40 | 5 | 2023-10-15 | 40000.00 |
| 1006 | Pooja Verma | 28 | NULL | 2022-04-01 | 62000.00 |
| 1007 | Suman Sharma | 33 | 1 | 2021-05-05 | 68000.00 |
| 1008 | Avinash Pandey | 27 | NULL | 2023-09-06 | 60000.00 |
| 1009 | Sonam Singh | 24 | NULL | 2020-08-09 | 65000.00 |
| 1010 | Faraz Khan | 35 | NULL | 2022-03-19 | 55000.00 |
| 1011 | Arunima | 24 | NULL | 2021-11-27 | 63000.00 |
+------+-----------------+-----+--------+-----------------+----------+
+--------+------+
| sub_id | sub |
+--------+------+
| 1 | DBMS |
| 2 | OS |
| 3 | SE |
| 4 | SNT |
| 5 | COA |
+--------+------+
19
(1) Single Row Sub-query – fetching teacher(s) whose salary is maximum
(2) Multiple Row Sub-query – fetching teachers who are alloted the subjects
20
Program 7
🔴 Code 👇
DECLARE
v_username VARCHAR2(20) := ‘r-tripathi’; -- could be promted by user
v_password VARCHAR2(20) := ‘raj@090806’; -- could be promted by user
BEGIN
IF v_username = db_username AND v_password = db_password THEN
DBMS_OUTPUT.PUT_LINE(‘Login Successful ! Welcome ‘ || v_username);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Login Failed ! Invalid Credentials‘);
END IF;
END;
/
🔴 Output 👇
21
Program 8
22
mysql> SELECT * FROM tbl_accounts;
+---------+-----------+-----------+----------+---------------+---------------+
| cust_id | cust_name | ac_number | balance | last_txn_type | last_txn_date |
+---------+-----------+-----------+----------+---------------+---------------+
| 1001 | A | 98765 | 40000.00 | debit | 2025-04-06 |
| 1002 | B | 54321 | 10000.00 | NULL | NULL |
+---------+-----------+-----------+----------+---------------+---------------+
2 rows in set (0.00 sec)
👉 The transaction between both the customers is NOT completed due to the invalid
‘ac_number’ of customer ‘B’. So any changes made during the transaction process could be
reverted back using the ROLLBACK statement as :
mysql> ROLLBACK;
Query OK, 0 row affected (0.00 sec)
23
mysql> SELECT * FROM tbl_accounts;
+---------+-----------+-----------+----------+---------------+---------------+
| cust_id | cust_name | ac_number | balance | last_txn_type | last_txn_date |
+---------+-----------+-----------+----------+---------------+---------------+
| 1001 | A | 98765 | 40000.00 | debit | 2025-04-06 |
| 1002 | B | 54321 | 30000.00 | credit | 2025-04-06 |
+---------+-----------+-----------+----------+---------------+---------------+
2 rows in set (0.00 sec)
👉 The transaction between both the customers is completed. So we could make those
changes permanent by using the COMMIT statement as :
mysql> COMMIT;
Query OK, 0 row affected (0.00 sec)
24
Program 9
👉 VIEWS are the virtual table created using SELECT query. It allows simplified access to
specific rows & columns of one or more tables without altering the original data.
👉 VIEWS does not store the data itself. The VIEW will be failed if the base table is
dropped, i.e., the table from which the VIEW is created. Some VIEWS are non-updatable.
👉 We can use the concept of VIEWS to hide out the sensitive columns / information. This
increases the security and provides a level of abstraction.
+------+-----------------+-----+--------+-----------------+----------+
| id | name | age | sub_id | date_of_joining | salary |
+------+-----------------+-----+--------+-----------------+----------+
| 1001 | Sharda Tiwari | 27 | 1 | 2020-03-20 | 70000.00 |
| 1002 | Shashwat Shukla | 36 | 2 | 2024-05-11 | 35000.00 |
| 1003 | Ritu Dwivedi | 29 | 3 | 2023-02-03 | 68000.00 |
| 1004 | Chetna Singh | 39 | 4 | 2025-02-10 | 65000.00 |
| 1005 | Rakesh Sharma | 40 | 5 | 2023-10-15 | 40000.00 |
| 1006 | Pooja Verma | 28 | NULL | 2022-04-01 | 62000.00 |
| 1007 | Suman Sharma | 33 | 1 | 2021-05-05 | 68000.00 |
| 1008 | Avinash Pandey | 27 | NULL | 2023-09-06 | 60000.00 |
| 1009 | Sonam Singh | 24 | NULL | 2020-08-09 | 65000.00 |
| 1010 | Faraz Khan | 35 | NULL | 2022-03-19 | 55000.00 |
| 1011 | Arunima | 24 | NULL | 2021-11-27 | 63000.00 |
+------+-----------------+-----+--------+-----------------+----------+
25
Program 10
👉 CURSORS are the mechanism to fetch multiple rows from a query and process them
one-by-one in PL / SQL. Since queries like SELECT returns multiple rows, then CURSORS
works as a pointer to handle the result set in row-by-row manner.
👉 CURSORS are of 2 types – An Implicit Cursor (managed by the Oracle itself, for e.g. :
normal SELECT or UPDATE query), and An Explicit Cursor (created and managed manually
by the users). This program demonstrates using the Explicit Cursor.
🔴 Code 👇
26
🔴 Output 👇
👉 ‘dual’ is a dummy table in Oracle used to select a value without access any real table.
👉 The ‘UNION’ or ‘UNION ALL’ statement is used to combine the results of two ‘SELECT’
queries. On executing the ‘UNION’ statement, the duplicate rows are removed from the
result, whereas on executing the ‘UNION ALL’ statement, it combines all the rows as-it-is
without removing the duplicates.
27