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

DBMS Pactical File SS

The document provides examples of SQL statements for various operations like creating and describing tables, inserting data, selecting data, sorting data, renaming tables, deleting rows and tables, and joining tables. It includes statements for creating tables with columns, primary keys, foreign keys and other constraints. Statements are also provided for altering tables by adding, modifying and dropping columns. Aggregate functions, comparison operators, logical operators and update statements are demonstrated.

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DBMS Pactical File SS

The document provides examples of SQL statements for various operations like creating and describing tables, inserting data, selecting data, sorting data, renaming tables, deleting rows and tables, and joining tables. It includes statements for creating tables with columns, primary keys, foreign keys and other constraints. Statements are also provided for altering tables by adding, modifying and dropping columns. Aggregate functions, comparison operators, logical operators and update statements are demonstrated.

Uploaded by

Rahul Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1|P ag e

1: Write a SQL Statement to Create a table.


CREATE TABLE students (

id INT PRIMARY KEY,

name VARCHAR(50) NOT NULL,

age INT CHECK (age > 0),

gender CHAR(1) CHECK (gender IN ('M', 'F')),

marks DECIMAL(5,2) CHECK (marks BETWEEN 0 AND 100)

);

OUTPUT:
2|P ag e

2: Write a SQL Statement for Describing the table.

 Using previous students table

DESCRIBE students;

OUTPUT:
3|P ag e

3: Write a SQL command for Inserting the values into table.

 To insert values in selective columns:

INSERT INTO students (id, name, marks) VALUES (1, 'Raj', 85.5);

 To insert values in selective columns:

INSERT INTO students VALUES (2, 'Priya', 18, 'F', 92.0);

 Inserting values

INSERT INTO students VALUES

(3, 'Shreya', 20, 'F', 99.0),

(4, 'Rahul', 18, 'M', 98.0);

OUTPUT:
4|P ag e

4: Write a SQL Statement to fetch the data from the table.

 Using previous students table

SELECT * FROM students;

OUTPUT:
5|P ag e

5: Write a SQL Statement Sorting the data.

 Sorting the Data by using order by

SELECT * FROM students ORDER BY marks DESC;

OUTPUT:
6|P ag e

6: Write a SQL Statement to rename the table.

 Rename the table students to Learners

ALTER TABLE students RENAME TO learners;

OUTPUT:
7|P ag e

7: Write a SQL Statement to delete a particular row.

 Inserting a dummy record :

INSERT INTO learners VALUES (5, 'Suresh', 22, 'M', 42.0);

SELECT * FROM learners;

OUTPUT:

 Delete a particular row:

DELETE FROM learners WHERE id = 5;

SELECT * FROM learners;

OUTPUT:
8|P ag e

8: Write a SQL Statement to delete all records from the table.

 Delete all records from the table:

DELETE FROM learners;

OUTPUT:
9|P ag e

9: Write a SQL Statement to drop the table.

 Drop the table from the database:

DROP TABLE learners;

OUTPUT:
10 | P a g e

10: Write a SQL statement using Aggregate Functions(MIN, MAX, AVG,


SUM,COUNT).

 Assuming the table is recreated

CREATE TABLE students (

id INT PRIMARY KEY,

name VARCHAR(50) NOT NULL,

age INT CHECK (age > 0),

gender CHAR(1) CHECK (gender IN ('M', 'F')),

marks DECIMAL(5,2) CHECK (marks BETWEEN 0 AND 100)

);

 Inserting values to the Table

INSERT INTO students VALUES

(1, 'Raj', 18, 'M', 93.0),

(2, 'Priya', 18, 'F', 92.0),

(3, 'Shreya', 20, 'F', 99.0),

(4, 'Rahul', 18, 'M', 98.0);

SELECT * FROM students;

OUTPUT:
11 | P a g e

 Aggregate functions

-- Find the minimum, maximum, average, and total marks of all students
SELECT MIN(marks), MAX(marks), AVG(marks), SUM(marks) FROM students;

OUTPUT:

 Count function
-- Find the number of students who have marks above 80
SELECT COUNT(*) FROM students WHERE marks > 80;

OUTPUT:
12 | P a g e

11: SQL operators.


-- Create table to perform SQL operations.

CREATE TABLE employees (

id INT PRIMARY KEY,

name VARCHAR(100),

salary DECIMAL(10, 2),

incentive DECIMAL(10, 2),

bonus DECIMAL(10, 2)

);

-- Insert some sample data

INSERT INTO employees (id, name, salary, incentive, bonus)

VALUES

(1, 'John', 50000.00, 2000.00, 500.00),

(2, 'Jane', 60000.00, 2500.00, 600.00),

(3, 'Alice', 55000.00, 2200.00, 550.00);

 Airthmatic Opertors(+,-,*,/,%)

-- Perform arithmetic operations and output the results

SELECT

id,

name,

salary,

incentive,

bonus,

salary + incentive + bonus AS total_pay, -- Total pay

salary * 0.1 AS ten_percent_of_salary, -- 10% of salary

salary - incentive AS net_salary, -- Net salary after deduction of incentive


13 | P a g e

salary * 0.05 AS five_percent_of_salary -- 5% of salary

FROM

employees;

OUTPUT:

 Compariosn Operators(=,<,>,<=,>=,<>)
SELECT
id,
name,
salary,
incentive,
bonus,
CASE
WHEN salary > 55000 THEN 'Above Average' -- Check if salary is above average
ELSE 'Below Average'
END AS salary_comparison,
CASE
WHEN incentive >= 2200 THEN 'Satisfactory' -- Check if incentive is satisfactory
ELSE 'Unsatisfactory'
END AS incentive_comparison,
CASE
WHEN bonus = 500 THEN 'Standard Bonus' -- Check if bonus is standard
ELSE 'Non-Standard Bonus'
END AS bonus_comparison
FROM
Employees;
OUTPUT:
14 | P a g e

 Logical Operators(AND,OR,NOT,ALL,ANY,IN,BETWEEN,EXSITS,LIKE)

ALL Operator

SELECT ALL *FROM Employees WHERE salary >= 60000;

OUTPUT:
15 | P a g e

12: Write a SQL statement using ALTER command with ADD, MODIFY
and DELETE keyword.
CREATE TABLE employees (

id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

age INT,

department VARCHAR(50)

);

OUTPUT:

 ADD a new column named email of type VARCHAR(100):


ALTER TABLE employees
ADD email VARCHAR(100);
 MODIFY the data type of the age column from INT to SMALLINT:
ALTER TABLE employees
MODIFY age SMALLINT;
 DELETE the column department:
ALTER TABLE employees
DROP COLUMN department;

OUTPUT:
16 | P a g e

13: Integrity Constraints(Primary,Unique ,Not Null Keys)

CREATE TABLE Employees (

EmployeeID INT PRIMARY KEY,

FirstName VARCHAR(50) NOT NULL,

LastName VARCHAR(50) NOT NULL,

VoterID VARCHAR(10) UNIQUE,

Email VARCHAR(100)

);

INSERT INTO Employees (EmployeeID, FirstName, LastName,VoterID, Email)

VALUES

(1, 'John', 'Doe', 'DK7652','john@example.com'),

(2, 'Jane', 'Smith', 'PL786','jane@example.com'),

(3, 'Alice', 'Johnson','GJ874' ,NULL);

OUTPUT:
17 | P a g e

14: Update Command.

 Taking Reference from Previous Employees Table.

UPDATE Employees
SET Email='Alice@gmai.com'
WHERE EmployeeID=3;

SELECT * FROM Employees;

OUTPUT:
18 | P a g e

15: SQL Joins.


-- Create Employees table

CREATE TABLE Employees (

EmployeeID INT,

Name VARCHAR(50),

DepartmentID INT

);

-- Insert data into Employees table

INSERT INTO Employees (EmployeeID, Name, DepartmentID)

VALUES

(1, 'John', 101),

(2, 'Alice', 102),

(3, 'Bob', 101),

(4, 'Sarah', 103);

-- Create Departments table

CREATE TABLE Departments (

DepartmentID INT,

DepartmentName VARCHAR(50)

);

-- Insert data into Departments table

INSERT INTO Departments (DepartmentID, DepartmentName)

VALUES

(101, 'IT'),

(102, 'HR'),
19 | P a g e

(103, 'Marketing');

SELECT * FROM Employees;

SELECT * FROM Departments;

OUTPUT:

 Inner Join

SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName

FROM Employees

INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;


20 | P a g e

 Left Join
SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName

FROM Employees

LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

 Right Join

SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName

FROM Employees

CROSS JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;


21 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy