You are on page 1of 10

The Difference Between the HAVING and WHERE Clauses in a

SQL Query
Though the HAVING clause specifies a condition that is similar to the purpose of a WHERE clause,
the two clauses are not interchangeable. Listed below are some differences to help distinguish
between the two:

1. The WHERE clause specifies the criteria which individual records must meet to be selcted
by a query. It can be used without the GROUP BY clause. TheHAVING clause cannot be
used without the GROUP BY clause.
2. The WHERE clause selects rows before grouping. The HAVING clause selects
rows after grouping.
3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain
aggregate functions.

PL/SQL(a product of Oracle) is the 'programming


language' extension of sql.
It is a full-fledged language although it is specially designed
for database centric activities.
Advantage : Of Cursor,

In pl/sql if you want perform some actions more than one records
you should user these cursors only. bye using these cursors you
process the query records. you can easily move the records and
you can exit from procedure when you required by using cursor
attributes.

disadvantage:

using implicit/explicit cursors are depended by sutiation. if the


result set is les than 50 or 100 records it is better to go for implicit
cursors. if the result set is large then you should use exlicit cursors.
other wise it will put burdon on cpu.

Disadvantage:
Each time we fetch a row from the cursor, it result a network round
trip, where as a normal select statement query make only
one round trip.
36. Explain how procedures
and functions are called in
a PL/SQL block ?
Function is called as part
of an expression.
sal := calculate_sal
('a822');
procedure is called as a
PL/SQL statement
calculate_bonus ('A822');

37. What is Overloading of


procedures?
The Same procedure
name is repeated with
parameters of different
datatypes and parameters
in different positions,
varying number of
parameters is called
overloading of procedures.

e.g. DBMS_OUTPUT
put_line
Exception is the error handling part of PL/SQL block. The types are Predefined and
user defined. Some of Predefined exceptions are.
CURSOR_ALREADY_OPEN
DUP_VAL_ON_INDEX
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
INVALID_NUMBER
LOGON_DENIED
NOT_LOGGED_ON
PROGRAM-ERROR
STORAGE_ERROR
TIMEOUT_ON_RESOURCE
VALUE_ERROR
ZERO_DIVIDE
OTHERS.
Cursor is a named private SQL area from where information can be accessed.
Cursors are required to process rows individually for queries returning multiple
rows.
Some scalar data types such as
NUMBER,
VARCHAR2,
DATE,
CHAR,
LONG,
BOOLEAN.
Some composite data types such as RECORD & TABLE.
If the application is running very slow? At what points you need to go about thedatabase
to improve the performance?
For improving performance, we need to check the sql statement blocks , because for every sql
satement execution transfor to sql engine and come back to plsq engine that process takes
time to process the plsql block.
Stored procedures are subprogrammes stored in the database and can be called
&executee multiple times wherein an application procedure is the one being used
for a particular application same is the way for function.

Both can be executed any number of times. Only difference is that stored
procedures/ functions are stored in database in complied format while the
application procedures/functions are not in precomplied format and at run time
has to be compiled.

Where the predefined_exceptions are


stored

In the standard package.


Procedures, Functions & Packages
ROWID - Hexa decimal number each and every row having unique.Used in
searching.

ROWNUM - It is a integer number also unique for sorting Normally TOP N


Analysys.

Other Psudo Column are

NEXTVAL,CURRVAL Of sequence are some exampls

psudo columns are default columns provided by oracle

How can a function retun more than one value in oracle with proper example?
yes we can use objects, arrays to return more than one value

Overloading of procedures

The Same procedure name is repeated with parameters of different datatypes and
parameters in different positions, varying number of parameters is called
overloading of procedures.
e.g. DBMS_OUTPUT put_line

Locking
Locking are mechanisms intended to prevent destructive interaction between
users accessing data. Locks are used to achieve.
COLUMN command define column headings & format data values.
JOIN is the form of SELECT command that combines info from two or more tables.
Types of Joins are Simple (Equijoin & Non-Equijoin), Outer & Self join.
Equijoin returns rows from two or more tables joined together based upon a
equality condition in the WHERE clause.
Non-Equijoin returns rows from two or more tables based upon a relationship
other than the equality condition in the WHERE clause.
Outer Join combines two or more tables returning those rows from one table that
have no direct match in the other table.
Self Join joins a table to itself as though it were two separate tables.
Savepoint is a point within a particular transaction to which you may rollback
without rolling back the entire transaction.
SET command changes the system variables affecting the report environment.
Transaction is defined as all changes made to the database between successive
commits.
Dynamic SQL enables you to write programs that reference SQL statements
whose full text is not known until runtime.
Currently, cursor variables are subject to the following restrictions:You cannot
declare cursor variables in a package spec. For example, the following declaration
is not allowed:CREATE PACKAGE emp_stuff AS TYPE EmpCurTyp IS REF CURSOR
RETURN emp%ROWTYPE; emp_cv EmpCurTyp; -- not allowedEND emp_stuff;You
cannot pass cursor variables to a procedure that is called through
a database link.If you pass a host cursor variable to PL/SQL, you cannot fetch
from it on the server side unless you also open it there on the same server
call.You cannot use comparison operators to test cursor variables for equality,
inequality, or nullity.You cannot assign nulls to a cursor
variable.Database columns cannot store the values of cursor variables. There is
no equivalent type to use in a CREATE TABLE statement.You cannot store cursor
variables in an associative array, nested table, or varray.Cursors and cursor
variables are not interoperable; that is, you cannot use one where the other is
expected. For example, you cannot reference a cursor variable in a cursor FOR
loop
SQL*Loader is a product for moving data in external files into tables in an
Oracle database. To load data from external files into an Oracle database, two
types of input must be provided to SQL*Loader : the data itself and the control
file. The control file describes the data to be loaded. It describes the Names and
format of the data files, Specifications for loading data and the Data to be loaded
(optional). Invoking the loader sqlload username/password controlfilename
<options>.
Cursor for loop implicitly declares %ROWTYPE as loop index,opens a cursor,
fetches rows of values from active set into fields in the record and closes when all
the records have been processed.
eg. FOR emp_rec IN C1 LOOP
salary_total := salary_total +emp_rec sal;
END LOOP;
Cursor for loop implicitly declares %ROWTYPE as loop index,opens a cursor,
fetches rows of values from active set into fields in the record and closes when all
the records have been processed.
eg. FOR emp_rec IN C1 LOOP
salary_total := salary_total +emp_rec sal;
END LOOP;
When we are trying to determine the sum of some columns in a result set we can
use Compute. This function is used for reporting purpose.
The PRAGMA EXECPTION_INIT tells the complier to associate an exception with an
oracle error. To get anerror message of a specific oracle error.
e.g. PRAGMA EXCEPTION_INIT (exception name, oracle error number)
% TYPE provides the data type of a variable or a database column to that
variable.
% ROWTYPE provides the record type that represents a entire row of a table or
view or columns selected in the cursor.
The advantages are : I. Need not know about variable's data type
ii. If the database definition of a column in a table changes, the data type of a
variable changes accordingly.
Question 9: What is hash cluster in Oracle?
Answer : This is one of my favorite Oracle Interview question. Hash cluster is one of the techniques to store
the table in a hash cluster to improve the performance of data retrieval .we apply hash function on the table
row’s cluster key value and store in the hash cluster. All rows with the same hash key value are stores
together on disk.key value is same like key of index cluster ,for data retrieval Oracle applies the hash
function to the row's cluster key value.

Question 10: What is Snap shot in Oracle database?


Answer : Snapshots are read-only copies of a master table located on a remote node which is periodically
refreshed to reflect changes made to the master table.

When we use hash cluster.

Hash clusters are better choice when a table is often queried with equality queries.
For such queries the specified cluster key value is hashed. The resulting hash key
value points directly to the area on disk that stores the specified rows.

What are dbase links


Private Database Link, Public Database Link & Network Database Link.
A database link is a named object that describes a path from one database to
another
Private database link is created on behalf of a specific user. A private database link
can be used only when the owner of the link specifies a global object name in a SQL
statement or in the definition of the owner's views or procedures.

A table is the basic unit of data storage in an ORACLE database. The tables of a
database hold all of the user accessible data. Table data is stored in rows and
columns.
Logical dbase structure,
Tablespaces and the Databases Schema Objects

Physical dbase structure.


ORACLE database is comprised of three types of files. One or more Data files, two
are more Redo Log files, and one or more Control files.

What does Opening a cursor do ?

- It executes the query and identifies the Result set

What does Fetching a cursor do ?

- It reads the Result Set row by row.

What does Closing a cursor do ?

- It clears the private SQL area and de-allocates the memory.

What are Cursor Variables ?

- Also called REF CURSORS.

- They are not tied to a single SQL. They point to any SQL area dynamically.

- Advantage is : You can declare variables at Client side and open them Server side. You
can thus centralize data retrieval.

Why use Cursor Variables?

- You can pass cursor RESULT SETS between PL/SQL stored programs and clients.

What are SQLCODE and SQLERRM ?

- Oracle Error code and detailed error message

- They are actually functions with no arguments, that can be used only in procedural
statements ( not SQL)

What are Pseudocolumns ?

- They are not actual columns. They are like Functions without arguments.

- They typically give a different value for each row.

- Examples: ROWNUM, NEXTVAL, ROWID, VERSION_STARTTIME

Why use Truncate over Delete while deleting all rows ?


- Truncate is efficient. Triggers are not fired.

- It deallocates space (Unless REUSE STORAGE is given).

What is a ROWID composed of ?

- It's a hexadecimal string representing the address of a row. Prior to Oracle 8, it's a
restricted rowid comprising block.row.file. Extended rowid ( the default on higher releases)
comprises data object number as well ( comprising the segment number ).

What is the use of a ROWID ?

- Retrieve data faster with ROWID.

- Shows you the physical arrangement of rows in the table.

- Also unique identifier for each row.

Can rows from two different tables have the same ROWID?

- Possible, if they are in a Cluster

What is ROWNUM and ROW_NUMBER ?

- ROWNUM is a pseudocolumn which is the number assigned to each row retrieved.

- ROW_NUMBER is an analytic function which does something similar, but has all the
capabilities of PARTITION BY and ORDER BY clauses..

What is an inline view?

- It's not a schema object

- It's a subquery in the FROM clause with an alias that can be used as a view within the SQL
statement.

What are Nested and Correlated subqueries ?

- The subquery used in WHERE clause is a nested subquery.

- If this subquery refers to any column in the parent statement, it becomes a correlated
subquery.

How do you retrieve a dropped table in 10g?

- FLASHBACK table <tabname> to BEFORE DROP

What are PSPs?

- PL/SQL Server Pages. Web pages developed in PL/SQL


What is an index-organized table?

- The physical arrangement of rows of this table changes with the indexed column.

- It's. in-short, a table stored like an index itself.

What is an implicit cursor?

- Oracle opens an implicit cursor to process each SQL statement not associated with an
explicit cursor.

Name a few implicit cursor attributes.

- %FOUND, %ROWCOUNT, %NOTFOUND, %ISOPEN, %BULK_ROWCOUNT,


%BULK_EXCEPTIONS

That's from me now.

A bitmap index is a special type of index that is primarily used in the Oracle database. Here
we will give a detailed explanation and tutorial of bitmap indexes so you can fully understand
how they work and when it makes sense to use a bitmap index.

Oracle bitmap indexes are very different from standard b-tree indexes. In bitmap
structures, a two-dimensional array is created with one column for every row in the table
being indexed. Each column represents a distinct value within the bitmapped index. This
two-dimensional array represents each value within the index multiplied by the number
of rows in the table. At row retrieval time, Oracle decompresses the bitmap into the RAM
data buffers so it can be rapidly scanned for matching values. These matching values are
delivered to Oracle in the form of a Row-ID list, and these Row-ID values may directly
access the required information.

The real benefit of bitmapped indexing occurs when one table includes multiple
bitmapped indexes. Each individual column may have low cardinality. The creation of
multiple bitmapped indexes provides a very powerful method for rapidly answering
difficult SQL queries.

For example, assume there is a motor vehicle database with numerous low-cardinality
columns such as car_color, car_make, car_model, and car_year. Each column contains
less than 100 distinct values by themselves, and a b-tree index would be fairly useless in
a database of 20 million vehicles. However, combining these indexes together in a query
can provide blistering response times a lot faster than the traditional method of reading
each one of the 20 million rows in the base table. For example, assume we wanted to find
old blue Toyota Corollas manufactured in 1981
Referential integrity is a relational database concept in which multiple tables share a
relationship based on the data stored in the tables, and that relationship must remain
consistent.

The concept of referential integrity, and one way in which it’s enforced, is best illustrated by
an example. Suppose company X has 2 tables, an Employee table, and an Employee Salary
table. In the Employee table we have 2 columns – the employee ID and the employee name.
In the Employee Salary table, we have 2 columns – the employee ID and the salary for the
given ID.

The term cardinality actually has two different meanings depending on the context of it’s
usage – one meaning is in the context of data modeling and the other meaning is in the
context of SQL statements. Let’s go through the first meaning – when the word cardinality is
used in the context of data modeling, it simply refers to the relationship that one table can
have with another table. These relationships include: many-to-many, many-to-one/one-to-
many, or one-to-one – whichever one of these characteristics a table has in relationship with
another table is said to be the cardinality of that table. An example will help clarify further.

e xample of Cardinality in SQL


The other definition of cardinality is probably the more commonly used version of the term. In
SQL, the cardinality of a column in a given table refers to the number of unique values that
appear in the table for that column. So, remember that the cardinality is a number. For
example, let’s say we have a table with a “Sex” column which has only two possible values of
“Male” and “Female”. Then, that “Sex” column would have a cardinality of 2, because there
are only two unique values that could possibly appear in that column – Male and Female.

Cardinality of a primary key


Or, as another example, let’s say that we have a primary key column on a table with 10,000
rows. What do you think the cardinality of that column would be? Well, it is 10,000. Because it
is a primary key column, we know that all of the values in the column must be unique. And
since there are 10,000 rows, we know that there are 10,000 entries in the column, which
translates to a cardinality of 10,000 for that column. So, we can come up with the rule that
the cardinality of a primary key column will always be equal to the number of records in the
same table.

What does a cardinality of zero mean?


Well, if a column has a cardinality of zero, it means that the column has no unique values.
This could potentially happen if the column has all NULL’s – which means that the column was
never really used anyways.
What is the point of having a foreign key?
Foreign keys are used to reference unique columns in another table. So, for example, a
foreign key can be defined on one table A, and it can reference some unique column(s) in
another table B. Why would you want a foreign key? Well, whenever it makes sense to have a
relationship between columns in two different tables.

In SQL, what are the differences between primary, foreign, and


unique keys?
The one thing that primary, unique, and foreign keys all have in common is the fact that each
type of key can consist of more than just one column from a given table. In other words,
foreign, primary, and unique keys are not restricted to having just one column from a given
table – each type of key can cover multiple columns. So, that is one feature that all the
different types of keys share – they can each be comprised of more than just one column,
which is something that many people in software are not aware of.

Of course, the database programmer is the one who will actually define which columns are
covered by a foreign, primary, or unique key. That is one similarity all those keys share, but
there are also some major differences that exist between primary, unique, and foreign keys.
We will go over those differences in this article. But first, we want to give a thorough
explanation of why foreign keys are necessary in some situations.

You might also like