Professional Documents
Culture Documents
SQL> select *
2 from
3 (
4 select ename
5 ,sal
6 ,rank() over (order by sal desc) ranking
7 from emp
8 )
9 where ranking = 4 -- Replace 4 with any value of N
DELETE FROM
table_name A
WHERE
a.rowid >
ANY (
SELECT
B.rowid
FROM
table_name B
WHERE
A.col1 = B.col1
AND
A.col2 = B.col2
);
update Table1 set "col" = (case "col" when 'male' then 'female'
else 'male' end);
declare
cursor cur_gender is
select * from gender
for update;
begin
for v_gen in cur_gender
loop
update gender
set sex = case v_gen.sex when 'male' then 'female' else 'male' end
where current of cur_gender;
end loop;
end;
/
Indexes of sql server are similar to the indexes in books, When there are thousands
of records in a table, retrieving information will take a long time, therefore indexes
are created on columns which are accessed frequently, so that the data can be
retrieved quickly.
2.WHAT IS VIEW?
View isasql statement which is stored in the database with an alias name.
Whenever we run view, sql executes in back end. Viewsare used to restrict the data
access of table.
A View is like a virtual table ,it does not physically exist, rather it created by a query
joining one or more tables.
A view can be created from one or more tables, which depends on the written sql
query to create view.
It is the physical address of the rows. Row number is the sequential number
allocated to each returned row during
Row id is automatically generated query execution.
unique id of a row and it is generated
at the time of insertion of row Row number is a dynamic value
automatically retrieved along with
select statement output.
TYPES OF CURSORS
1.Implicit cursors 2.explicit cursors
1.Stored procedure may or may not 1.functions should returns the values.
returns values.
2.function can be called from
2. These cannot be used in a select select/where/having clause
/where/having clauses.
3.these can be executed using
3.stored procedure can be run EXECUTE or EXEC command.
independently. (These are part of sql commands)
5.these can have Input and output 5.functions have only i/p parameters.
parameters.
It does not free the space containing It will free the space containing the
the table. table.
TRUNCATE
DELETE FROM
table_name A
WHERE
a.rowid >
ANY (
SELECT
B.rowid
FROM
table_name B
WHERE
A.col1 = B.col1
AND
A.col2 = B.col2
);
(OR)
Using self join
Surrogate is a substitution for the natural primary key in the dimension tables.
Surrogate key is a numeric number that uniquely identifies records in the dimension
table.
It is also know as artificial or identity key in data warehouse.
It is useful because the natural primary key can change and this makes updates
more difficult.
1.Unique key allows null values BUT Primary key does not allow null values.
2.We can have multiple unique keys in a table but primary key is one and only one.
3.Primary key in itself is a unique key.
4.Primary can be referenced to other table as foreign key.
But with some restrictions, it can possible to insert/update/delete from a base table
using view.
2 For views with “WITH CHECK” options enabled, its important to keep in mind that
the data begin inserted qualifies in the WHERE clause of the view.
3.if a NOT NULL column that does not have DEFAULT clause is omitted from the
view, then a row can be inserted.
4.if the view was created by using an expression ,such as DECODE ,then rows can
not be inserted into or updated in the base table using the view.
16.QUERY TO FIND THE TOP N th SALARIES OF EMLOYEES?
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC ) a
ORDER BY salary
TYPES:
1 Equi join
2 Non-equi join
3 Self join
4 Natural join
5 cross join
6 outer join
Left outer
Right outer
Full outer
7 Inner join
8 Using clause
9 On clause