Question Paper About Procedure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

*************************************************************

****
Consider the following two query results:
SELECT count(*) AS total FROM orders;
+-------+
| total |
+-------+
| 100 |
+-------+
SELECT count(*) AS cust_123_total FROM orders WHERE customer_id = '123';
+----------------+
| cust_123_total |
+----------------+
|

15

+----------------+

Given the above query results, what will be the result


of the query below?
SELECT count(*) AS cust_not_123_total FROM orders WHERE customer_id <> '123'

ANS IS :-

It give 85 record .

Given the following tables:


sql> SELECT * FROM runners;
+----+--------------+
| id | name

+----+--------------+
| 1 | John Doe

| 2 | Jane Doe

| 3 | Alice Jones |
| 4 | Bobby Louis |
| 5 | Lisa Romero |
+----+--------------+

sql> SELECT * FROM races;


+----+----------------+-----------+
| id | event

| winner_id |

+----+----------------+-----------+
| 1 | 100 meter dash | 2

| 2 | 500 meter dash | 3

| 3 | cross-country | 2

| 4 | triathalon

| NULL

+----+----------------+-----------+

What will be the result of the query below?


SELECT * FROM runners WHERE id NOT IN (SELECT winner_id FROM races)
ANS is:

No Output of the above question.


Explain your answer and also provide an alternative
version of this query that will avoid the issue that it
exposes.
Here in race table column filled winner_id have null value in id no 4 .
So if any operation occure with null, result is not comming . if we
assign any id (ex. 4) instaid of null then output will be

id| name
1 | John Doe
5 | Lisa Romero

alternative version of this query :


SELECT * FROM runners WHERE id NOT IN (SELECT winner_id FROM races
WHERE winner_id IS NOT null)

Given two tables created and populated as follows:


CREATE TABLE dbo.envelope(id int, user_id int);
CREATE TABLE dbo.docs(idnum int, pageseq int, doctext varchar(100));

INSERT INTO dbo.envelope VALUES


(1,1),
(2,2),
(3,3);

INSERT INTO dbo.docs(idnum,pageseq) VALUES


(1,5),
(2,6),
(null,0);

What will the result be from the following query:


UPDATE docs SET doctext=pageseq FROM docs INNER JOIN envelope ON
envelope.id=docs.idnum
WHERE EXISTS (
SELECT 1 FROM dbo.docs
WHERE id=envelope.id
);

Explain your answer.


it will update the column doctext of table docs with value 5 and 6 in
1st and 2nd column respectively.

What is wrong with this SQL query? Correct it so it

executes properly.
SELECT Id, YEAR(BillingDate) AS BillingYear
FROM Invoices
WHERE BillingYear >= 2010;

ANS:
allias name can not be used here we nee d to write
SELECT Id, YEAR(BillingDate) AS BillingYear
FROM Invoices
WHERE YEAR(BillingDate) >= 2010;

Given these contents of the Customers table:


Id

Name

ReferredBy

John Doe

NULL

Jane Smith

NULL

Anne Jenkins

Eric Branford

NULL

Pat Richards

Alice Barnes

Here is a query written to return the list of customers


not referred by Jane Smith:
SELECT Name FROM Customers WHERE ReferredBy <> 2;

Name:
Pat Richards

What will be the result of the query? Why? What


would be a better way to write it?
It can not execute with null value.

SELECT Name FROM Customers WHERE Name <> 'Jane Smith' and ReferredBy
=1

or
SELECT Name FROM Customers WHERE ReferredBy = NULL OR ReferredBy <> 2

Assume a schema of Emp ( Id, Name, DeptId ) , Dept ( Id,


Name).
If there are 10 records in the Emp table and 5 records
in the Dept table, how many rows will be displayed in
the result of the following SQL query:
Select * From Emp, Dept

Explain your answer.


50 Row will be created.
it is 10x 5= 50 record are generated.
Given a table SALARIES, such as the one below, that
has m = male and f = femalevalues. Swap
all f and m values (i.e., change all f values to m and
vice versa) with a single update query and no
intermediate temp table.
Id Name Sex Salary
1 A

2 B

3 C

4 D

2500
1500
5500
500

UPDATE SALARIES SET SEX=


CASE Sex

WHEN 'M' THEN 'F'


WHEN 'F' THEN 'M'
END

Given two tables created as follows


create table test_a(id numeric);

create table test_b(id numeric);

insert into test_a(id) values


(10),
(20),
(30),
(40),
(50);

insert into test_b(id) values


(10),
(30),
(50);

Write a query to fetch values in table test_a that are


and not in test_b without using the NOT keyword.
select * from test_a
except
select * from test_b;

Given a table TBL with a field Nmbr that has rows with
the following values:
1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1

Write a query to add 2 where Nmbr is 0 and add 3

where Nmbr is 1.
update tbl set Nmbr=
case Nmbr
when 0 then Nmbr +2
else Nmbr +3
end

Write a SQL query to find the 10th highest employee


salary from an Employee table. Explain your answer.
(Note: You may assume that there are at least 10
records in the Employee table.)
select top(1) sal from (select distinct TOP (10) Sal from Emp ORDER BY
Sal desc)as empp order by sal asc

What is stored procedure?

A stored procedure is precompiled set of one or more SQl statements that stored
on sql server.

Explain about the difficulties faced by the database developer in implementing


pre compiled statements?

the main difficulties is that we don't know how many argument will be provided
during compile time.

Does storing of data in stored procedures increase the access time? Explain?

yes , the storing of data in stored procedure increase the access time.
Because Data can be precompiled and stored in Stored procedures. This reduces
the time access .

State about the security aspects of stored procedures?

Code is hidden, controlling access to data , statements in a stored procedure only

need to be written once in sql server by writting in stored procedures.

State the different extensions for stored procedures?

.dbo

Explain about the implementation of stored procedures?

a stored procedure is a set of (SQL) statements with an assigned name that's


stored in the database in compiled form so that it can be shared by a number of
programs.
The use of stored procedures can be helpful in controlling access to data .
Create once and call it N number of times.

What is the difference between a user defined function and a Stored procedure?

procedure:
1.
It is is a set of predefined (SQL) statements which are compiled once and
stored on the sql server.
2.

Stored procedure have an option to return zero on n value.

3.

Procedure have input and output parameter.

4.

Procedure can not be called from function.

user defined function:


1.

It is compiled and executed every time when it is called.

2.

But function must return a value.

3.

Function have only input parameter.

4.

Function can be called from procedure.

What are the uses of stored procedure?

Hacking is not possible.


Fetching data more faster than code written in application.
Easyly modification can be possible.

what is the Difference between View and Stored Procedure?

Where the procedures are stored in database?

we can see by Expand


"Database" -----------> Programmibility----------->Stored Procedure
Actually stored procedure are stored in the database data dictionary.

How can we call stored procedures inside store procedures?

EXECUTE <PROCEDURE NAME>

what is the difference between stored procedures and stored functions

What are external procedures ? Why and when they are used?

You might also like