0% found this document useful (0 votes)
18 views

DBMS Lab File

The document provides examples of SQL queries to illustrate various SQL concepts like selecting columns, order by clause, arithmetic operators, aggregate functions, group by clause, null values, aliases, concatenation, distinct rows, joins, outer joins and creating tables with constraints.

Uploaded by

anyt6126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

DBMS Lab File

The document provides examples of SQL queries to illustrate various SQL concepts like selecting columns, order by clause, arithmetic operators, aggregate functions, group by clause, null values, aliases, concatenation, distinct rows, joins, outer joins and creating tables with constraints.

Uploaded by

anyt6126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Illustration of SELECTING OF ALL COLUMNS

Syntax:
SELECT *
FROM <table_name>;

Query:
SELECT *
FROM Customers;

Output:

Conclusion : Using the above query command we can select all columns

Illustration of SELECTING OF SPECIFIC 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

Prashant Shah: 29600


Illustration of ORDER BY Clause

Syntax:
SELECT column1, column2, ..., columnN
FROM table_name
ORDER BY column_name [ASC | DESC];
Query:
SELECT *
FROM Orders
ORDER BY OrderDate DESC;
Output:

Concnclusion : By using bove query we can perform order by clause

Illustration of Arithmetic operators.


Syntax:
SELECT expression
FROM table_name;
Query:
Addition ->SELECT ProductID, Price + 10 AS IncreasedPrice
FROM Products;
Substraction-> SELECT OrderID, TotalAmount - Discount (10) AS DiscountedAmount
FROM Orders;
Multiplication-> SELECT Quantity * UnitPrice AS TotalPrice
FROM OrderDetails;
Division -> SELECT AverageRating = SUM(Rating) / COUNT(*)
FROM Reviews;

Output:

Conclusion : By using above query, we can perform arithmetic operation


Prashant Shah :29600
Illustration of Operator Precedence in Arithmetic expression.

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:

Conclusion: Using above query we can use aggregate Functions


Prashant Shah : 29600
Illustration of GROUP BY clause and Restricting Group Results with the HAVING Clause

Query :
--> SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
--> SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;

--> SELECT COMPANY, COUNT(*)


FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
--> SELECT COMPANY, SUM(COST)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING SUM(COST)>=170;
Table:

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:

Conclusion: From above query we can select NUll value


Prashant Shah : 29600
Illustration of Using Column Aliases
Syntax:
--> SELECT column_name AS alias_name
FROM table_name;
--> SELECT column_name(s)
FROM table_name AS alias_name;
Query:
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
--> SELECT * FROM Customers AS Persons;

Table:

Output:

Conclusion: Using above query we can use aliases

Illustration of Concatenation Operator


Syntax:
CONCAT(string1, string2, ...., string_n)
Query:
SELECT CONCAT('SQL', ' is', ' fun!');
Output:

Conclusion : using above query we can use concat operator

Prashant Shah : 29600


Illustration of displaying distinct rows and table Structure.

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.

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);
-- Left outer join query
SELECT * FROM departments
LEFT OUTER JOIN employees ON departments.dept_id = employees.dept_id;
-- Right outer join query
SELECT * FROM departments
RIGHT OUTER JOIN employees ON departments.dept_id = employees.dept_id;
-- Full outer join query
SELECT * FROM departments
FULL OUTER JOIN employees ON departments.dept_id = employees.dept_id;

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:

Conclusion : Using above command we can create table


Prashant Shah: 29600
Illustration of SubQuery

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:

Conclusion: Using Above query we can use sub query


Prashant Shah: 29600

You might also like