0% found this document useful (0 votes)
12 views29 pages

Dbms Practical File.

The document discusses various SQL queries including those for data definition and manipulation, logical operators, group by functions, aggregate functions, subqueries, joins, extracting data from multiple tables, transactions, and PL/SQL programs for number swapping, finding the greatest of three numbers, displaying sums of even and odd numbers in a range, generating prime numbers in a range, and finding Armstrong numbers in a range.
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)
12 views29 pages

Dbms Practical File.

The document discusses various SQL queries including those for data definition and manipulation, logical operators, group by functions, aggregate functions, subqueries, joins, extracting data from multiple tables, transactions, and PL/SQL programs for number swapping, finding the greatest of three numbers, displaying sums of even and odd numbers in a range, generating prime numbers in a range, and finding Armstrong numbers in a range.
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/ 29

Q1: Write the queries for Data Manipulation and Data Definition

Language ?

List of DML Queries: -

 CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other database
objects.

Query:-

CREATE Database Database_Name;

 DROP Command:-
DROP is a DDL command used to delete/remove the database objects from the SQL database.
We can easily remove the entire table, view, or index from the database using this DDL
command.

Query:-
DROP DATABASE Database_Name;

 ALTER Command: -
ALTER is a DDL command which changes or modifies the existing structure of the database,
and it also changes the schema of database objects.

We can also add and drop constraints of the table using the ALTER command.

Query:-
ALTER TABLE name_of_table ADD column_name column_definition;
 TRUNCATE Command: -
TRUNCATE is another DDL command which deletes or removes all the records from the table.
This command also removes the space allocated for storing the table records.

Query:-

TRUNCATE TABLE Table_Name;

 RENAME Command:-
RENAME is a DDL command which is used to change the name of the database table.

Query:-
RENAME TABLE Old_Table_Name TO New_Table_Name;
Q2: Write SQL queries using logical operations and
operators?

 AND Operator :-
Query :-

SELECT * FROM Table_name WHERE First_Condition AND Second_Condition;

 IN Operator :-

Query :-

SELECT * FROM Table_name WHERE Column_name IN ('Condition1',


'Condition2');

 NOT Operator :-

Query :-

SELECT * FROM Table_name WHERE Column_name NOT LIKE 'Condition';

 OR Operator :-

Query :-

SELECT * FROM Table_name WHERE First_condition OR Second_condition;

 LIKE Operator :-

Query :-

SELECT * FROM Table_name WHERE Column_name LIKE 'Condition';


Q3: Write SQL query using group by function?

Sample Table:-

SQL GROUP BY Function:-

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country;

The following SQL statement lists the number of customers in each country.
Q4: Write SQL queries for aggregate functions?

1. COUNT FUNCTION

o COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.

Query:-

1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )

2. SUM Function

Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.

Query:-

1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )

3. AVG function

The AVG function is used to calculate the average value of the numeric type. AVG function returns the
average of all non-Null values.

Query:-

1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
4. MAX Function

MAX function is used to find the maximum value of a certain column. This function determines the largest
value of all selected values of a column.

Query:-

1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )

5. MIN Function

MIN function is used to find the minimum value of a certain column. This function determines the smallest
value of all selected values of a column.

Query:-

1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Q5: Write SQL queries for sub queries, nested queries?

 Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement. The basic syntax is
as follows –

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])

 Subqueries with the INSERT Statement


Subqueries also can be used with INSERT statements. The INSERT statement uses the data
returned from the subquery to insert into another table. The selected data in the subquery can
be modified with any of the character, date or number functions.
The basic syntax is as follows.
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]

 Subqueries with the UPDATE Statement


The subquery can be used in conjunction with the UPDATE statement. Either single or multiple
columns in a table can be updated when using a subquery with the UPDATE statement.
The basic syntax is as follows.
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
 Subqueries with the DELETE Statement
The subquery can be used in conjunction with the DELETE statement like with any other
statements mentioned above.
The basic syntax is as follows.
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Q6: Write an SQL query to implement JOINS ?

Dummy tables:-

Query:-
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
OUTPUT:-
Q7: Write a query for extracting data from more than one table?

Costumer 1 table:
OUTPUT:-
Q8: Write a query to understand the concepts for
ROLLBACK, COMMIT & CHECKPOINTS?

SQL COMMIT:-

Consider the Employees tables that containing the following records:-

Query:-
DELETE from Employees Where age = 27;
COMMIT;
Select * from Employees;

OUTPUT:-
Q9: Write a PL/SQL program to swap two numbers without
using third variable?

DECLARE
-- declare variable num1, num2
-- of datatype number
num1 NUMBER;
num2 NUMBER;
BEGIN
num1 := 1000;

num2 := 2000;

-- print result before swapping


dbms_output.Put_line('Before');

dbms_output.Put_line('num1 = '
|| num1
||' num2 = '
|| num2);

-- swapping of numbers num1 and num2


num1 := num1 + num2;

num2 := num1 - num2;

num1 := num1 - num2;

-- print result after swapping


dbms_output.Put_line('After');

dbms_output.Put_line('num1 = '
|| num1
||' num2 = '
|| num2);
END;
-- Program End

OUTPUT:-
Before
num1 = 1000 num2 = 2000
After
num1 = 2000 num2 = 1000
Q10: Write a PL/SQL program to find the biggest of three given
number?
--To find the greatest number
-- among given three numbers
DECLARE
--a assigning with 46
a NUMBER := 46;
--b assigning with 67
b NUMBER := 67;
--c assigning with 21
c NUMBER := 21;
BEGIN
--block start
--If condition start
IF a > b
AND a > c THEN
--if a is greater then print a
dbms_output.Put_line('Greatest number is '
||a);
ELSIF b > a
AND b > c THEN
--if b is greater then print b
dbms_output.Put_line('Greatest number is '
||b);
ELSE
--if c is greater then print c
dbms_output.Put_line('Greatest number is '
||c);
END IF;
--end if condition
END;
--End program

Output:

Greatest number is 67
Q11: Write a PL/SQL program to display “sum” of even numbers and
“sum” of odd numbers in the given range?

For odd:-

-- display all odd number from 1 to n


DECLARE
-- declare variable num
num NUMBER(3) := 1;
sum1 NUMBER(4) := 0;
BEGIN
WHILE num <= 5 LOOP
-- display odd number
dbms_output.Put_line(num);

-- the sum of all odd numbers


sum1 := sum1 + num;

--next odd number


num := num + 2;

-- end loop
END LOOP;
dbms_output.Put_line('Sum of all odd numbers is '|| sum1);
END;

OUTPUT:-

1
3
5
Sum of all odd numbers is 9
FOR EVEN:-

-- Display all even number from 1 to n


DECLARE
-- Declare variable num
num NUMBER(3) := 2;
sum1 NUMBER(4) := 0;
BEGIN
WHILE num <= 5 LOOP

-- Display even number


dbms_output.Put_line(num);

-- Sum of even numbers


sum1 := sum1 + num;

-- Next even number


num := num + 2;

-- End loop
END LOOP;

-- Display even number


dbms_output.Put_line('Sum of even numbers is ' || sum1);
END;

OUTPUT:-

2
4
Sum of even numbers is 6
Q12: Write a PL/SQL program to generate prime no’s in a given range?

DECLARE
--the upper limit and the lower limit are taken as user inputs.
low number(2);
high number(2);
n number(2);
m number(2);
c number(20);
BEGIN
dbms_output.put_line('Enter the lower and higher limit:');
low:=&low;
high:=&high;
--The main operation happens in this loop
for n IN low.. high
loop
c:=0;
for m IN 1.. n
loop
if mod(n, m)=0 then
c:=c+1;
end if;
end loop;
--the number of divisors for each number in the range is counted and
then checked.
if c<=2 then
dbms_output.put_line(n||'\n');
end if;
end loop;

END;
OUTPUT:-
Input:
Enter the lower and higher limit:1 10
Output: 2
3
5
7
Q13: Write a PL/SQL program to find the Armstrong numbers in a
given range?
declare
-- declare variable n, s,r, len
-- and m of datatype number
n number:=1634;
s number:=0;
r number;
len number;
m number;

begin
m := n;

len := length(to_char(n));

-- while loop till n>0


while n>0
loop
r := mod(n , 10);
s := s + power(r , len);
n := trunc(n / 10);
end loop;

if m = s
then
dbms_output.put_line('yes');
else
dbms_output.put_line('no');
end if;

end;

OUTPUT:-
YES
Q14: Write a PL/SQL program to generate palindrome numbers in a
given range?
declare

-- declare variable n, m, temp


-- and temp of datatype number
n number;
m number;
temp number:=0;
rem number;

begin
n:=5432112345;
m:=n;
-- while loop with condition till n>0
while n>0
loop
rem:=mod(n,10);
temp:=(temp*10)+rem;
n:=trunc(n/10);
end loop; -- end of while loop here

if m = temp
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
OUTPUT:-
True
Q15: Write a PL/SQL program to find the Factorial of a given number?

declare
-- it gives the final answer after computation
fac number :=1;

-- given number n
-- taking input from user
n number := &1;

-- start block
begin

-- start while loop


while n > 0 loop

-- multiple with n and decrease n's value


fac:=n*fac;
n:=n-1;
end loop;
-- end loop

-- print result of fac


dbms_output.put_line(fac);

-- end the begin block


end;

OUTPUT:-(IF input is 5)

120
Q16: Write the PL/SQL programs to create the procedure to find
Fibonacci series?

declare

-- declare variable first = 0,


-- second = 1 and temp of datatype number
first number := 0;
second number := 1;
temp number;

n number := 5;
i number;

begin

dbms_output.put_line('Series:');

--print first two term first and second


dbms_output.put_line(first);
dbms_output.put_line(second);

-- loop i = 2 to n
for i in 2..n
loop
temp:=first+second;

first := second;
second := temp;

--print terms of fibonacci series


dbms_output.put_line(temp);
end loop;

end;
--Program End

OUTPUT:-

0 1 1 2 3 5
Q19: Write a PL/SQL block that will display the employee details along
with salary using Cursors?

DECLARE
z_empid employees.employee_id%TYPE;
z_empname employees.first_name%TYPE;
z_salary employees.salary%TYPE;
CURSOR employee_cursor IS -- declaring a cursor
SELECT employee_id,
first_name,
salary
FROM employees;

BEGIN
OPEN employee_cursor; -- opening the cursor
LOOP
FETCH employee_cursor -- fetching records from the cursor
INTO z_empid,
z_empname,
z_salary;
EXIT
WHEN employee_cursor%NOTFOUND;
IF (z_salary > 8000) THEN
dbms_output.Put_line(z_empid
|| ' '
|| z_empname
|| ' '
|| z_salary);
ELSE
dbms_output.Put_line(z_empname
|| ' salary is less then 8000');
END IF;
END LOOP;
CLOSE employee_cursor; --closing the cursor
END;
/

OUTPUT:-

100 Steven 24000


101 Neena 17000
102 Lex 17000
103 Alexander 9000
Bruce salary is less then 8000
David salary is less then 8000
Valli salary is less then 8000
Diana salary is less then 8000
108 Nancy 12008
109 Daniel 9000
110 John 8200
Ismael salary is less then 8000
Jose Manuel salary is less then 8000
Luis salary is less then 8000
114 Den 11000
Q20: Write a Cursor to display the list of employees who are
working as a Managers or Analyst?

DECLARE
CURSOR emp_job_cur IS
SELECT job_id,
job_title
FROM jobs
WHERE job_id IN (SELECT job_id
FROM employees)
ORDER BY 2;
CURSOR employees_cur (
emp_jobid VARCHAR) IS
SELECT first_name,
Count(start_date) no_of_jobs
FROM employees e
left outer join job_history jh USING (employee_id)
WHERE e.job_id = emp_jobid
GROUP BY first_name
ORDER BY 1;
BEGIN
FOR emp_job_rec IN emp_job_cur LOOP
dbms_output.Put_line('---------------------------------------');
dbms_output.Put_line('Job Title: '||emp_job_rec.job_title);
dbms_output.Put_line('---------------------------------------');

FOR employee_list IN employees_cur( emp_job_rec.job_id)


LOOP
dbms_output.Put_line(' '
|| Rpad(employee_list.first_name, 20)
|| employee_list.no_of_jobs);
END LOOP;
END LOOP;
END;
/
OUTPUT:-

---------------------------------------
Job Title: MANAGERS
---------------------------------------
Daniel 0
Ismael 0
John 0
Jose Manuel 0
Luis 0
---------------------------------------
Job Title: ANALYSTS
---------------------------------------
Shelley 0
---------------------------------------

You might also like