DBMS 6,7,8,9
DBMS 6,7,8,9
NO : 6
QUERYING USING ANY, ALL, IN, EXISTS, NOT EXISTS,UNION,
INTERSECT
DATE:
AIM
To write SQL queries using ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, and INTERSECT
operators.
PROCEDURE
1. Use the ANY operator to compare a value to any value in a list.
2. Use the ALL operator to compare a value to all values in a list.
3. Use the IN operator to check if a value exists in a list.
4. Use the EXISTS operator to check the existence of a subquery result.
5. Use the NOT EXISTS operator to check the non-existence of a subquery result.
6. Use the UNION operator to combine the results of two queries.
7. Use the INTERSECT operator to return the common results of two queries.
8. Document the SQL queries used for each operator.
9. Execute the queries in the SQL environment.
10. Validate the results by checking the retrieved data.
QUERIES:
CREATE A TABLE OF EMPLOYEES:
Code:
CREATE TABLE employees
( employee_id INT PRIMARY
KEY, employee_name
VARCHAR(100),
department_id INT,
salary DECIMAL(10,
2)
);
19 717823F137
OUTPUT:
OUTPUT:
ANY OPERATOR:
Code:
SELECT
employee_name FROM
employees
WHERE salary > ANY (SELECT salary FROM employees WHERE department_id = 1);
OUTPUT:
20 717823F137
ALL OPERATOR:
Code:
SELECT
employee_name FROM
employees
WHERE salary > ALL (SELECT salary FROM employees WHERE department = 'HR');
OUTPUT:
IN OPERATOR:
Code:
SELECT employee_name, department_id
FROM employees
WHERE department_id IN ('HR', 'Finance', 'IT');
OUTPUT:
EXISTS OPERATOR:
Code:
SELECT
employee_name FROM
employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE d.department_id = e.department_id AND
d.department_name = 'HR');
OUTPUT:
22 717823F137
WHERE NOT EXISTS (SELECT 1 FROM departments d WHERE d.department_id = e.department_id
AND d.department_name = 'HR');
OUTPUT:
UNION OPERATOR:
Code:
SELECT
e.employee_name FROM
employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name = 'HR'
UNION
SELECT
e.employee_name FROM
employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name = 'IT';
OUTPUT:
RESULT:
Thus, we created sql queries to implement data querying using any, all, in, exists, not exists,union,
intersect
23 717823F137
EX.NO : 7
QUERYING USING AGGREGATE FUNCTIONS (COUNT, SUM,
AVG, MAX, AND
DATE:
MIN)
AIM
To write SQL queries using aggregate functions COUNT, SUM, AVG, MAX, and MIN to perform
calculations on data.
PROCEDURE
1. Use the COUNT function to count the number of rows in a table.
2. Use the SUM function to calculate the total of a numeric column.
3. Use the AVG function to calculate the average value of a numeric column.
4. Use the MAX function to find the maximum value in a column.
5. Use the MIN function to find the minimum value in a column.
6. Combine aggregate functions with GROUP BY to calculate grouped data.
7. Document the SQL queries used for each aggregate function.
8. Execute the queries in the SQL environment.
9. Validate the results by checking the calculated data.
10. Ensure the queries perform as expected with various data sets.
QUERIES:
CREATE THE SALES_DATA TABLE:
CREATE TABLE sales_data (
id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100),
category VARCHAR(50), price
DECIMAL(10, 2),
quantity INT,
sale_date DATE
);
24 717823F137
OUTPUT:
OUTPUT:
-- Calculate the total revenue from product sales (price * quantity) SELECT
SUM(price * quantity) AS total_revenue FROM sales_data;
OUTPUT:
OUTPUT:
26 717823F137
OUTPUT:
OUTPUT:
Code:
-- Find the minimum price of any product
SELECT MIN(price) AS min_price FROM sales_data;
Code:
-- Calculate the total quantity and average price for each product category
SELECT category, SUM(quantity) AS total_quantity, AVG(price) AS
average_price FROM sales_data
GROUP BY category;
27 717823F137
OUTPUT:
Code:
-- Count the number of rows in the sales_data table SELECT
COUNT(*) AS row_count FROM sales_data;
OUTPUT:
RESULT:
Thus, we created sql queries to querying using aggregate functions (count, sum, avg, max, and min)
28 717823F137
EX.NO : 8
QUERYING USING GROUP BY, HAVING
DATE:
AIM
To write SQL queries using GROUP BY and HAVING clauses to group and filter data.
PROCEDURE
1. Use the GROUP BY clause to group rows that have the same values in specified columns.
2. Use aggregate functions with GROUP BY to calculate grouped data.
3. Use the HAVING clause to filter groups based on a specified condition.
4. Document the SQL queries used for GROUP BY and HAVING.
5. Execute the queries in the SQL environment.
6. Validate the results by checking the grouped and filtered data.
7. Combine GROUP BY with different aggregate functions.
8. Ensure the queries perform as expected with various data sets.
9. Optimize the queries for better performance.
10. Test the queries with different group by columns.
QUERIES:
USE THE GROUP BY CLAUSE TO GROUP ROWS THAT HAVE THE SAME VALUES IN
SPECIFIED COLUMNS:
OUTPUT:
OUTPUT:
OUTPUT:
32 717823F137
SELECT Product, SUM(QuantitySold) AS
TotalQuantity FROM Sales
GROUP BY Product
HAVING SUM(QuantitySold) > 10;
OUTPUT:
OUTPUT:
OUTPUT:
34 717823F137
CREATE INDEX idx_product ON Sales(Product);
OUTPUT:
RESULT:
EX.NO : 9
CREATE AND MANAGE VIEWS AND INTEGRITY
DATE: CONSTRAINTS
Thus, we created sql queries to implement querying using group by, having
35 717823F137
AIM:
To create and manage views and integrity constraints in a relational database.
PROCEDURE:
1. Use the CREATE VIEW command to create views.
2. Use the SELECT statement to query data from views.
3. Use the DROP VIEW command to delete views if needed.
4. Define integrity constraints such as PRIMARY KEY, FOREIGN KEY, UNIQUE, and CHECK.
5. Add constraints to existing tables using the ALTER TABLE command.
6. Verify the creation of views and constraints by querying the database metadata.
7. Document the SQL queries used for creating and managing views and constraints.
8. Execute the queries in the SQL environment.
9. Validate the results by checking the structure and data of the created views.
10. Ensure data integrity by testing different scenarios.
QUERIES:
36 717823F137
OUTPUT:
Code:
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT, order_date DATE NOT NULL,
total_amount DECIMAL(10, 2) CHECK
(total_amount >= 0), FOREIGN KEY
(customer_id) REFERENCES customers(customer_id)
);
OUTPUT:
CREATE A VIEW:
Code:
CREATE VIEW customer_orders
AS SELECT
c.customer_id
, c.first_name,
37 717823F137
c.last_name,
38 717823F137
o.order_id,
o.order_date,
o.total_amoun
t
FROM
customers c JOIN
orders o ON c.customer_id = o.customer_id;
OUTPUT:
DROP VIEWS:
DROP VIEW customer_orders;
OUTPUT:
39 717823F137
CHECK CONSTRAINTS:
Code:
SELECT *
FROM information_schema.table_constraints
WHERE table_schema = 'your_database_name'
AND table_name IN ('customers', 'orders');
RESULT:
Thus, we created sql queries to implement create and manage views and integrity constraints
40 717823F137