DBMS Lab File
DBMS Lab File
Syntax:
SELECT *
FROM <table_name>;
Query:
SELECT *
FROM Customers;
Output:
Conclusion : Using the above query command we can select all columns
Sytax:
SELECT column1, column2, ..., columnN
FROM table_name;
Query:
SELECT ProductName, Price, StockLevel
FROM Products;
Output:
Conclusion: Using the above query command we can select specific columns
Syntax:
SELECT column1, column2, ..., columnN
FROM table_name
ORDER BY column_name [ASC | DESC];
Query:
SELECT *
FROM Orders
ORDER BY OrderDate DESC;
Output:
Output:
Query:
DECLARE
a number(2) := 20;
b number(2) := 10;
c number(2) := 15;
d number(2) := 5;
e number(2) ;
BEGIN
e := (a + b) * c / d; -- ( 30 * 15 ) / 5
dbms_output.put_line('Value of (a + b) * c / d is : '|| e );
e := ((a + b) * c) / d; -- (30 * 15 ) / 5
dbms_output.put_line('Value of ((a + b) * c) / d is : ' || e );
e := (a + b) * (c / d); -- (30) * (15/5)
dbms_output.put_line('Value of (a + b) * (c / d) is : '|| e );
e := a + (b * c) / d; -- 20 + (150/5)
dbms_output.put_line('Value of a + (b * c) / d is : ' || e );
END;
/
Output:
Illustration of aggregate functions.
Syntax:
COUNT( [ALL|DISTINCT] expression )
SUM( [ALL|DISTINCT] expression )
AVG( [ALL|DISTINCT] expression )
MAX( [ALL|DISTINCT] expression )
MIN( [ALL|DISTINCT] expression )
Query:
-> SELECT COUNT(*)
FROM PRODUCT_MAST;
-> SELECT SUM(COST)
FROM PRODUCT_MAST;
-> SELECT AVG(COST)
FROM PRODUCT_MAST;
-> SELECT MAX(RATE)
FROM PRODUCT_MAST;
-> SELECT MIN(RATE)
FROM PRODUCT_MAST;
Table:
Output:
Query :
--> SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
--> SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;
Output:
Conclusion: By using Above query, we can illustrate GROUP BY clause and Restricting Group Results with the
HAVING Clause
PRASHANT SHAH : 29600
Illustration of a Null Value
Syntax:
--> SELECT column_names
FROM table_name
WHERE column_name IS NULL;
--> SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Query:
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
--> SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
Table:
Output:
Table:
Output:
Query:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(20));
-- Inserting sample data
INSERT INTO students (id, name, class) VALUES
(1, 'Alice', 'A'),
(2, 'Bob', 'B'),
(3, 'Charlie', 'A'),
(4, 'David', 'C'),
(5, 'Emma', 'A'),
(6, 'Frank', 'B');
-- Displaying distinct class values
SELECT DISTINCT class FROM students;
-- Using DESCRIBE
DESCRIBE students;
--or below code only for table show
-- Using SHOW CREATE TABLE
SHOW CREATE TABLE students;
Output:
Conclusion : using above query we can displaying distinct rows and table Structure
Prashant Shah :29600
Illustration of Use of BETWEEN, LIKE, IN, AND, OR , NOT Operator
Query:
Table creation:
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
price DECIMAL(10, 2),
category VARCHAR(50));
Between operator:
SELECT * FROM products
WHERE price BETWEEN 100 AND 500;
IN Operator:
SELECT * FROM employees
WHERE department IN ('IT', 'HR', 'Finance');
LIKE Operator:
SELECT * FROM products
WHERE product_name LIKE 'T%';
AND Operator:
SELECT * FROM employees
WHERE salary BETWEEN 30000 AND 50000
AND department = 'IT';
OR Operator:
SELECT * FROM employees
WHERE department = 'IT' OR department = 'Finance';
NOT Operator:
SELECT * FROM employees
WHERE department <> 'HR';
Output:
`
Conclusion : WE can use above command for Between,In,Like,And,OR,NOt operator
Prashant Shah :29600
Illustration of Creating Cross Joins and Natural Join.
Query:
-- Sample tables: departments and employees
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50));
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id));
-- Inserting sample data
INSERT INTO departments (dept_id, dept_name) VALUES
(1, 'IT'),(2, 'HR');
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES
(101, 'John Doe', 1),(102, 'Jane Smith', 1),(103, 'Michael Johnson', 2);
-- Cross join query
SELECT * FROM departments CROSS JOIN employees;
-- Natural join query
SELECT * FROM departments NATURAL JOIN employees;
Output:
Conclusion: By using Above query, we can create Cross Joins and Natural Join
Prashant Shah : 29600
Illustration of Creating Joins with the USING Clause and ON Clause.
Query:
-- Sample tables: departments and employees
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50));
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id));
-- Inserting sample data
INSERT INTO departments (dept_id, dept_name) VALUES
(1, 'IT'),(2, 'HR');
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES
(101, 'John Doe', 1),(102, 'Jane Smith', 1),(103, 'Michael Johnson', 2);
-- Join query using USING clause
SELECT * FROM departments
JOIN employees USING (dept_id);
-- Join query using ON clause
SELECT * FROM departments
JOIN employees ON departments.dept_id = employees.dept_id;
Output:
Conclusion: Using above query we can create with USING Clause and ON Clause.
Prashant Shah :29600
Illustration of LEFT OUTER JOIN , Right OUTER JOIN , Full OUTER JOIN.
Output:
Conclusion: Using Above query, we can Illustration of LEFT OUTER JOIN, Right OUTER JOIN, Full OUTER JOIN .
Prashant Shah : 29600
Illustration of Creating table with enforcement of integrity constraints primary key, not null,
unique,check, Referential integrity.
Query:
-- Creating students table with primary key, not null, and unique constraints
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE);
-- Creating courses table with foreign key constraint referencing students table
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50) NOT NULL,
instructor VARCHAR(50) NOT NULL,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id));
-- Adding a check constraint to ensure course_id is greater than 0
ALTER TABLE courses ADD CHECK (course_id > 0);
Output:
Query:
-- Sample tables: students and courses
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
email VARCHAR(100));
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
instructor VARCHAR(50));
-- Inserting sample data into students table
INSERT INTO students (student_id, student_name, email) VALUES
(1, Prashant shah ', prashant@prashantshah.info.np'),(2, 'Jane Smith', 'jane@example.com'),
(3, 'Michael Johnson', 'michael@example.com');
-- Inserting sample data into courses table
INSERT INTO courses (course_id, course_name, instructor) VALUES
(101, 'Mathematics', 'Professor X'),(102, 'History', 'Professor Y'),
(103, 'Science', 'Professor Z');
--use a subquery to find students enrolled in the "Mathematics" course:
SELECT student_id, student_name, email
FROM students
WHERE student_id IN (
ELECT student_id
FROM courses
WHERE course_name = 'Mathematics');
Output: