0% found this document useful (0 votes)
17 views18 pages

DBMS P-1

Uploaded by

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

DBMS P-1

Uploaded by

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

Rudra Saini

11723210031

PRACTICAL -1

AIM:- Introduction to SQL


INTRODUCTION:
 SQL stands for Structured Query Language. It is used for storing and managing data
in relational database management system (RDMS).
 It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
 All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL
as their standard database language.
 SQL allows users to query the database in a number of ways, using English-like
statements.

KEY CONCEPT IN SQL:


 PRIMARY KEY- A primary key, also called a primary keyword, is a column in a
relational database table that's distinctive for each record. It's a unique identifier, such
as a driver's license number, telephone number with area code or vehicle identification
number (VIN). A relational database must have only one primary key.
 FOREIGN KEY- A foreign key (FK) is a column or group of columns in a table that
links data in two tables in a relational database. It acts as a cross-reference between
tables by referencing the primary key of another table.

DATA TYPE SUPPORTED IN SQL-


 Numeric Types:
o INT - Integer.
o TINYINT - Small integer.
o SMALLINT - Medium-sized integer.
o MEDIUMINT - Medium-sized integer.
o BIGINT - Large integer.
o FLOAT - Single-precision floating-point.
o DOUBLE - Double-precision floating-point.
o DECIMAL - Fixed-point decimal.
 Date and Time Types:

1
Rudra Saini
11723210031

o DATE - Date (YYYY-MM-DD).


o TIME - Time (HH:MM:SS).
o DATETIME - Date and time (YYYY-MM-DD HH:MM:SS).
o TIMESTAMP - Timestamp.
o YEAR - Year (YYYY).
 String Types:
o CHAR - Fixed-length character string.
o VARCHAR - Variable-length character string.
o TEXT - Variable-length text.
o BINARY - Fixed-length binary string.
o VARBINARY - Variable-length binary string.
o BLOB - Variable-length binary large object.
 Bit Type:
o BIT - Bit field type.
 Boolean Type:
o BOOLEAN - Synonym for TINYINT(1).

Basic SQL Commands :-

 CREATE It is used to create a new table in the database.

2
Rudra Saini
11723210031

Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAMES DATATYPES [ ...]);

 DROP: It is used to delete both the structure and record stored in the table.

Syntax: DROP TABLE table_name [cascade constraint];

 INSERT: The INSERT statement is a SQL query.

Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, val
ue2, value3, .... valueN);

 DELETE: It is used to remove one or more row from a table.

Syntax: DELETE FROM table_name [WHERE condition];

 SELECT: It is used to select the attribute based on the condition described by


WHERE clause.
Syntax : SELECT expressions FROM TABLES WHERE conditions;
 UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax :
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [W
HERE CONDITION]

Result: We have studied the basics of SQL.

Pre-Viva questions:
1. What does SQL stand for?
2. What are the applications of SQL in real world?
3. What is the difference between primary key and foreign key?

Post-Viva questions:
1. Explain the different types of numeric data types supported by SQL.
2. Name the string data types available in SQL.

3
Rudra Saini
11723210031

PRACTICAL – 2

AIM- To study Basic SQL commands (create database, create table, use, drop,
insert) and execute the queries using these commands.
COMMANDS-
1. CREATE and USE:
Syntax: Create Database ;
Use <name>;

2. CREATE TABLE:
Syntax: Create table <table_name>(<attribute_name> datatype{e.g. Char(20)},
<name2>datatype,…});

3. INSERT:
Syntax: Insert into <table_name> values (‘data1’,’data2’);

4. SELECT:
Syntax: Select * from <table_name> where <attribute_name>=’data1’;

4
Rudra Saini
11723210031

5. DELETE FROM:
Syntax:
Delete from <table_name> where <attribute_name>=’data1’;

6.DROP:
Syntax:
Drop table <table_name>;

Result: All commands are executed successfully.

Pre-Viva questions:
1. What is INSERT command?
2. What are the different ways to use INSERT INTO command?

Post-Viva questions:
1. When would you use DROP command in SQL?
2. Difference between DROP and TRUNCATE command.
3. CREATE and DROP commands belong to which category?

5
Rudra Saini
11723210031

PRACTICAL – 3

AIM:- To study the viewing commands (select & update) and execute the queries using these
commands.

DDL Commands Used:


 SELECT – Select command is used to retrieve data from one or more tables in a
database.
Syntax- SELECT col1, col2
FROM table_name
WHERE condition;
 UPDATE – Update command is used to modify existing records in a table.
Syntax—UPDATE table_name
SET col1 = new_value1,
col2 = new_value2
WHERE condition;
SQL Queries:-
1. Select * from Motorbike;
Output :-

6
Rudra Saini
11723210031

2. Update Motorbike
Set model=2024
Where name= “Pulsar RS”;
Output :-

3. Select * from Motorbike;


Output :-

4. Update Motorbike set colour=’white’ where name =’GT 650’;


Output :-

5. Update Motorbike set Engine=’1200CC’ where name =’Pulsar RS’;


Output :-

7
Rudra Saini
11723210031

6. Update Motorbike set model=(model+2);

Result: All commands are executed successfully.

Pre-Viva questions:
1. What is select command used for?
2. How to specify condition with select command?
3. What is update command?

Post-Viva questions:
1. Difference between update and alter command
2. Update and select commands belong to which category?

8
Rudra Saini
11723210031

PRACTICAL – 4

AIM:- To study commands to modify structure of table (alter, delete, add, modify, drop)
and execute the queries using these commands.
Table used to execute commands:

SQL Queries:
1. ALTER TABLE - ADD:
SYNTAX: ALTER TABLE table_name ADD column_name datatype;
OUTPUT:

2. ALTER TABLE - DROP:


SYNTAX: ALTER TABLE table_name DROP column_name;
OUTPUT:

9
Rudra Saini
11723210031

3. ALTER TABLE - MODIFY:


SYNTAX: ALTER TABLE table_name MODIFY column_name
new_datatype new constraint;
OUTPUT:

4. DELETE:
SYNTAX: DELETE from table_name WHERE condition;
OUTPUT:

Result:
All commands are executed successfully.

10
Rudra Saini
11723210031

Pre-Viva questions:
1. What is drop command?
2. What is Alter command used for?
3. Delete and modify commands belong to which category?
Post-Viva questions:
1. How to specify condition with delete command?
2. Difference between drop and delete command?

11
Rudra Saini
11723210031

PRACTICAL – 5

AIM :- To study the commands that involve compound conditions (and ,or ,in, not in,
between , not between , like , not like ) and execute the queries using these commands.
Commands Used:
 AND – It is used to filter records based on more than one condition. It displays a
record if all conditions are true.
Syntax- SELECT col1, col2
FROM table_name
WHERE condition1 and condition2.
 OR – It displays a record if any of the condition are true.
Syntax—SELECT col1, col2
FROM table name
WHERE condition1 or condition2.
 IN – It is used to specify multiple values in a where clause.
Syntax - SELECT column_name
FROM table_name
WHERE column name IN (value1, value2…)
 NOT IN – It is used to replace a group of argument using<>(or!=)operator that are
combined with a find .
Syntax- SELECT column_name
FROM table_name
WHERE column name between Value1 and Value2;
 BETWEEN – The operator selects values within a given range .
Syntax – SELECT column_name
FROM table_name
WHERE column_name between Value1 AND Value2;
 NOT BETWEEN – It is used for getting the values which is outside of the specified
range .
Syntax - SELECT column_name
FROM table_name
WHERE column_name not between Value 1 and Value2;

12
Rudra Saini
11723210031

 LIKE- It is used to search for a specified pattern in a column . The % represents


zero,one or multiple character.
Syntax – SELECT column_name
FROM table_name
WHERE column_name like pattern;
 NOT LIKE – It is used on a column which is of type char and we get records that
don’t
Match tha pattern .
Syntax – SELECT column_name
FROM table_name
WHERE column not like pattern;
Database Schema:
Eg: students (student id, first name, last name, age).
SQL Queries:-
1. Select * from student where age BETWEEN 18 and 19;
OUTPUT:-

2. Select *from student where age>18 or age<20;


OUTPUT:-

13
Rudra Saini
11723210031

3. Select * from student where stu_id in(2,3);


OUTPUT:-

4. Select*from student where stu_id not in (1,3);


OUTPUT:-

5. Select * from student where age =19 AND S_id= 6;


OUTPUT:-

6 . Select* from student where age NOT BETWEEN 20 and 24;


OUTPUT:-

14
Rudra Saini
11723210031

7 . Select*from student where first_name like ‘R%’;


OUTPUT:-

8 . SELECT *from student where first_name not like A%;


OUTPUT:-

Result: All commands are executed successfully.

PRE-VIVA QUESTIONS:
1. Where is AND used for?
2. Difference between IN and NOT IN?
3. In like why we use %?

PRE-VIVA QUESTIONS:
1. When would you use BETWEEN in SQL?
2. What is like and not like?

PRACTICAL – 6
15
Rudra Saini
11723210031

AIM:- To study the aggregate functions (sum, count, max, min, average) and execute the
queries using these commands.
Aggregate Functions:
Functions where the values of multiple rows are grouped together as input based on certain
criteria to form a single value of more significant meaning are known as AGGREGATE
FUNCTIONS.

 SUM(): Function returns the total sum of a numeric column.


Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;

 COUNT():Function returns the number of rows that matches a specific criterion.


Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

 MAX():Function returns the largest value of the selected column.


Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;

 MIN():Function returns the smallest value of the selected column.


Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;

 AVG():Function returns the average value of a numeric column.


Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;

Table Used :

16
Rudra Saini
11723210031

SQL Queries:-
1) Find the average salary of the employees:
SELECT AVG(Salary) FROM Employee;

OUTPUT:-

2) Find out the number of unique locations for different departments:


SELECT COUNT(D_No) FROM Employees;
OUTPUT:-

3) Find out the maximum commission given to an employees:


SELECT MAX(Salary) FROM Employees;
OUTPUT:

17
Rudra Saini
11723210031

4) Find the total commission given to employees:


SELECT SUM(Salary) FROM Employees;
OUTPUT:

5) Print the details of the employee having the minimum gross salary:
SELECT * FROM Employees
WHERE Salary IN (SELECT MIN(Salary) FROM Employees);
OUTPUT:-

Result: All commands are executed successfully.

PRE-VIVA QUESTIONS:
1. What are aggregate functions in SQL and why are they important in database
management?
2. What is the COUNT function in SQL? How does the COUNT function handle NULL
values?
3. Explain the usage of SUM function in SQL.

POST-VIVA QUESTIONS:
1. Illustrate MAX and MIN functions in SQL.
2. Explain the difference between COUNT(*) and COUNT(column_name) in SQL.
3. When would you use the AVG function in SQL, and what type of columns does it
operate on?

18

You might also like