UNIT-1 Solved
UNIT-1 Solved
13. Assume that a procedure has to be called in JDBC, which statement is used for that
purpose?
The Informix JDBC driver provides the Statement, PreparedStatement, and CallableStatement
methods, which can be used to execute stored procedures. The method we use depends on the
characteristic of the stored procedure. For example, if the stored procedure returns a single
value, you should use a JDBC Statement object. The following table provides some guidelines
for what method to use for which stored procedure type.
Stored procedure type JDBC method
Stored procedure requires no IN or OUT
parameters Use a Statement object
Stored procedure with IN parameters Use a PreparedStatement object
Stored procedure with IN and OUT
parameters Use a CallableStatement object
18. Create Tables as follows by choosing appropriate data type and set the necessary
primary and foreign key constraints: Product (Prodid, Prodesc, Price, Stock)
Purchase (Purid, Proid, qty), Add a column customer name in Purchase table.
Create table product( prodid number(10), prodesc varchar(10), price number(10), stock
number(10), primary key (prodid));
Create table purchase(purid number(10), proid number(10) references product, qty
number(10),primary key (purid));
Alter table purchase add (cust_name varchar(10));
SQL Query select leader from Jedi-Teams, Jedi, Government where apprentice = name and name
= leader and side = 'dark'
Relational algebra
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,3) = 0
SELECT *
FROM ( SELECT rownum rn, empno, ename
FROM emp
) temp
WHERE MOD(temp.rn,2) = 1
26. Consider there are certain strings available in a database. Mention the command
used to fetch first 5 characters of the string?
To obtain partial characters from a string SUBSTRING function can be used. The syntax
and parameters are mentioned below.
The SQL Server SUBSTRING function syntax is as follows:
SUBSTRING (expression, position, length)
Parameters:
expression: Input source string
position: Is an integer value that specifies the initial position from which the characters
can be extracted from the given expression. The first position of an expression is always
starting with 1. The initial position can be a negative integer.
length: Is a positive integer value. It specifies the ending limit and determines how many
characters are going to be extracted from the given expression.
29. Write a SQL query to fetch employee names having salary greater than or equal to
5000 and less than or equal 10000.
User will use BETWEEN in the 'where' clause to return the empId of the employees with salary
satifying the required criteria and then use it as subquery to find the fullName of the employee
form EmployeeDetails table.
SELECT FullName
FROM EmployeeDetails
WHERE EmpId IN
(SELECT EmpId FROM EmpolyeeSalary
WHERE Salary BETWEEN 5000 AND
10000);
31. Examine the structure of the EMPLOYEES table. You issue the following command:
INSERT INTO EMPLOYEES (employee_id , first_name , job_id) VALUES (5100,
'BRUCE', 'CLERK');Assuming that there is a duplicate value check constraint on the
EMPLOYEE_ID column, what will be the outcome of the above statement?
The ORA error message is triggered when a unique constraint has been violated.
Essentially the user causes the error when trying to execute an INSERT or UPDATE statement
that has generated a duplicate value in a restricted field. The error can commonly be found when
a program attempts to insert a duplicate row in a table.
This statement will throw an error namely a 'Constraint violated' ORA error , since the
row with values "5100, BRUCE, CLERK" already exists in the table, the insert statement with
same data set is not possible.
33. Write an SQL query to Select all employees from department numbers 7369, 7499
and Display all the details of the records whose employee name starts with „S‟
Select * from emp where deptno in(7369,7499);
Use SELECT FROM WHERE syntax.
Select should include all in the given format.
From should include employee
where empname like ‗s%
„; Table:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
Query:
SELECT DNAME FROM DEPT WHERE DEPTNO IN ( SELECT DEPTNO FROM
EMP WHERE ENAME LIKE 'S%'
Answer: 7369 SMITH
34. Write an SQL query to determine the max and min salary and rename the column
as max_salary and min_salary.
Steps:
1. Use the MIN & MAX aggregate function in select clause for finding the maximum and
minimum salary from the database.
2. Rename the column as min_sal & max_sal.
Ans: select min(sal) ―min_salary, max(sal) ―max_salary from emp;
Regulation: 2019 Academic Year 2022-2023
35. Write an SQL query to list out the employee names who will celebrate their birthdays
during current month and to display the names and dob of all employees who were born
in February.
Steps to obtain the name of employees who are born in february
Use “select” from clause.
Use to_char functions on name in select clause to obtain the result.
Ans: select empname from emp where to_char (dob,„MON„) like to_char (sysdate,
‗MON„); Ans: select empname , dob from emp where to_char (dob,„MON„)=„FEB„;
36. Write a query using having clause for Listing the number of customers in
each country. Only include countries with more than 10 customers.
SELECT COUNT(Id), Country
FROM Customer
GROUP BY Country
HAVING COUNT(Id) > 10
54. Why SELECT * is not preferred in embedded SQL programs?
SELECT* is not used in embedded SQL due to following reasons:
If the table structure is changed (a field is added ), the program will have to be
modified.
Program might retrieve the columns which it might not use, leading on I/O overhead.
The chance of an index only scan is lost.
38. Write the SQL query to Display total salary spent for employees and Display total
salary spent for each job category.
The usage of SQL GROUP BY clause is, to divide the rows in a table into smaller
groups. The GROUP BY clause is used with the SQL SELECT statement.The grouping can
happen after retrieves the rows from a table.When some rows are retrieved from a grouped result
Regulation: 2019 Academic Year 2022-2023
The query for obtaining total salary spent for employees , GROUP BY clause is used with the
SELECT statement to make a group of rows based on the values of a specific column or
expression. The SQL AGGREGATE function can be used to get summary information for every
group and these are applied to an individual group.
40. You need to find the salaries for all the employees who have a higher salary than the
Vice President of a company 'ABC'. What is the query used to obtain the result?
(Consider the table structure as given)
SQL> DESC employees
Name Null? Type
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
The Query below obtains the inner sub-query gives the VP's salary as a result to the outer query.
Part-B
5. Let relations r1(A,B,C) and r2(C,D,E) have the following properties: r1 has 20,000
tuples, r2 has 45,000 tuples, 25 tuples of r1 fit on one block and 30 tuples of r2 fit on one
block Estimate the number of block transfers and seeks required, using each of the
following join strategies for r1∞r2:
i. Nested loop join
ii. Block nested loop join
iii. Merge join
iv. Hash join
Solutions:
r1 needs 800 blocks, and r2 needs 1500 blocks. Let us assume M pages of memory. If
M > 800, the join can easily be done in 1500 + 800 disk accesses, using even plain nested-
i. Nested-loop join: Using r1 as the outer relation we need 20000 ∗ 1500 + 800 = 30, 000,
loop join. So we consider only the case where M ≤ 800 pages.
800 disk accesses, if r2 is the outer relation we need 45000 ∗ 800 + 1500 = 36, 001, 500
disk accesses.
ii. Block nested-loop join: A block-nested loop (BNL) is an algorithm used to join two
relations in a relational database. This algorithm is a variation on the simple nested loop
join used to join two relations and (the "outer" and "inner" join operands, respectively).
Block nested loop join requires no indices and can be used with any kind of join
Solution: If r1 is the outer relation, we need ⌈ 800 M−1 ⌉ ∗ 1500 + 800 disk accesses, if r2 is
condition.
the outer relation we need ⌈ 1500 M−1 ⌉ ∗ 800 + 1500 disk accesses.
iii. Merge-join: The Merge Join operator is one of four operators that join data from two
input streams into a single combined output stream. However, it requires all input data to
be sorted by the join columns. Often this means that a Merge Join can‟t be used without
adding extra Sort operators. The Merge Join operator supports all ten logical join
operations: inner join; left, right, and full outer join; left and right semi and anti semi join;
as well as concatenation and union. The algorithm requires at least one equality-based
join predicate.
Solution: Assuming thatr1 and r2 are not initially sorted on the join key, the total sorting cost
inclusive of the output is Bs = 1500(2⌈logM−1(1500/M)⌉+2) + 800(2⌈logM−1(800/M)⌉ + 2)
disk accesses. Assuming all tuples with the same value for the join attributes fit in memory, the
total cost is Bs + 1500 + 800 disk accesses.
iv. Hash-join: The Hash Join algorithm is a good choice, if the tables are large and there is
no usable index. Like the Sort Merge Join algorithm, it is a two-step process. The first
step is to create an in-memory hash index on the left side input. This step is called the
build phase. The second step is to go through the right side input one row at a time and
find the matches using the index created in step one. This step is called the probe phase.
Solution: We assume no overflow occurs. Since r1 is smaller, we use it as the build relation and
r2 as the probe relation. If M > 800/M, i.e. no need for recursive partitioning, then the cost is
Regulation: 2019 Academic Year 2022-2023
3(1500+800) = 6900 disk accesses, else the cost is 2(1500 + 800)⌈logM−1(800) − 1⌉ + 1500
+ 800 disk accesses.
table-name is the name of the table from which the information is retrieved.
column_list includes one or more columns from which data is retrieved.
The code within the brackets is optional.
SQL Alter DataBase
The ALTER DATABASE Statement is used to change characteristics of a
database.After creating a database, we can change its properties by executing ALTER
DATABASE statement. The user should have admin privileges for modifying a database.
SQL ALTER DATABASE Statement:
The Syntax for the ALTER DATABASE Statement is:
ALTER DATABASE database_name
[COLLATE collation_name ]
In the above query,
database_name - is the name of the database to be created
- collation_name - - is the default collation for the database
collation_name - This is an optional field and if not provided thanDefault Collation is
assigned to database.
Solutions:
a. CREATE TABLE
stu_details(
reg_no int, stu_name varchar(25),
DOB date,
address nvarchar(50),
city varchar(35));
Regulation: 2019 Academic Year 2022-2023
b. CREATE
TABLEMark
_details( reg_n
o int, mark1
int, mark2 int,
mark3 int, total
int));
i. ALTER TABLE mark_details ADD (average long);
ii. SELECT DATEDIFF(MM,DOB,GetDate()) as Months FROM stu_details
iii. ALTER TABLE stu_details DROP(address);