Interview Questions for SQL Beginners & experienced
What is data ?
Any facts need to be stored for future purpose Called data.
Data cannot give any information.
Data can structured or unstructured.
Structured 🡪empname , address .
Unstructured 🡪empphoto , addmap .
What is information ?
The processed data can be called as information.
This information gives meaning.
What is database ?
It is a collection of interrelated data, i.e is it is always stored Data along with it’s relations.
What is metadata ?
It is data which describes the properties or characteristics of End user data and context of the data.
What is entity ?
It is significance about the information need to know.
Difference between char and varchar2 ?
What is difference b/w long and raw ?
What is diff b/w raw and longraw ?
What is diff b/w blob and clob ?
What is null ?
Null value means the value is unavailable, unassigned, unknown or inapplicable.
What is order by clause ?
It is used to sort the rows. It is last clause in sql stmt. It default is ascending.
Howmany predefined function functions , what are they ?
They are two types of predefined functios.
What is diff b/w ceil and floor ?
Diff b/w replace and translate functions ?
What is diff b/w substr and instr functions ?
What is diff b/w pad and trim?
What is diff delete and drop ?
What is diff of truncate and delete?
What is diff of where and having clause?
What is group by clause ?
It is operate on set of rows to give one result per group. It is returns summary information for each group. All group by clause columns list may or may not used in select clause. Extra non group functional columns also declared in group by clause.
What is diff of length and vsize function?
Length function is returns number of characters in given string where Vsize function returns number of bytes in a given string.
what is constraint and howmany cons having?
Constrains are used to impose of business rules in organization.it is allows to enter only valid data.
They are 5 types 1) primary key.
2)unique key.
3) check.
4) foreign key.
5) not null.
23) what is diff between primary key and foreign key?
What is check constraint ?
Check constraint is cannot enter values until given condition true .
What is diff b/w primary and unique constraint?
In table only one primary key is valid. In pk 2 database objects are generated. Primary key cannot accepts duplicates and null values.
Where unique is number of columns are allowed to apply. Here 2 database objects are created. Unique is cannot allows duplicates but allows null values.
What are the joins how many joins having?
Join is mechanism , by using of join to get the data from more than one table. Generally one table cannot get complete information by making right decision then we go for using join and get complete information to making a right decision otherwise we can go to sub queries.
joins having 4
8i features like 🡪equi,nonque,outer,self and cartesion join
9i features like 🡪natural,inner,left,right outer and full outer join.
what is equijoin ?
To get matched rows from tables ,called equijoin .
what is self join?
Self join is indicates join the table to itself. Same table appears from clause and is must followed by table alias.
By performs self join ina table must have a at least 2 columns with same data type and same values.
what is diff of case and decode functions?
Is there a limit to the number of arguments that you can have in one DECODE statement? I’m getting an error, “ORA-00939: too many arguments for function”.
Yes, the maximum number of components that you can have in a DECODE function is 255. This includes the expression, search, and result arguments.
The DECODE function returns a value that is the same data type as the first result in the list.
If the first result is NULL, then the return value is converted to VARCHAR2.
If the first result has a data type of CHAR, then the return value is converted to VARCHAR2.
If no matches are found, the default value is returned.
If default is omitted and no matches are found, then NULL is returned.
What is diff. between unique index and unique key?
What is the purpose of sub query?
To define the set of to be inserted into target table of an insert or created table stmts.to define one or more values to be assigned to existing rows in update stmts.to define the set of rows to be included in a view or Mview insert or create table stmt.
To get the information by combining the two query’s,placing one query inside the other query.
What is the inline view query?
A subquery in the from clause of select statement.
What is the diff btwn In and exist ?
Which join can give more performance ?
Natural join gives more.
If Natural Join doesn’t exist then self join gives more performance.
Diff btwn Union And union all?
What is view?
View is a logical data. It doesn’t store the data in db. It is also known as stored select statement.
View can be also be created within the view.
View which can be created without base table known as force view.
Diff btwn view and materialized view?
What is MV?
MV is a physical db Object which stores the data permanently in db. MV is used to Data warehouses.
Syntax:
Create Materialized view m_v
[Immediate /Deffered] refresh[fast/complete /force]
on [comit/demand] as select…tb;
If we want to use refresh fast command we must create mv with logon with same table name or else it throws error.
When we create refresh complete , the performance can get degraded.
When mv create with refresh force the Materialized view logon created or else it behaves like a refresh complete.
Syntax:
Exec dbms_mview.refresh(tb name);
Indexes:
What is a Index? And when idexes are created?
Index is a db obj. indexes are explicitly created on table to retrieve the data very fast . indexes are activated when index columns are used in where clause.
When indexes are created .
Column contains wide range of values.
Columns contains large number of null values.
One or more columns are frequently used in a where clause or join condition.
Table is large and most queries are expected to retrieve less t of rows.
When indexes are not created .
Table is small.
The columns are frequently not used as condition in a query.
The table is updated frequently.
By default b-tree indexes are created when primary or unique constraints are applied in columns. Index names also generated by .
🡪 Do not create Btree index on those columns which contain many repeated values. In this case create BITMAP indexes on these columns.
50) what is bitmap indexes ?
A) Bitmap indexes are indicates each distinct key. Which is used to stores the rowid’s associated with a key value as a bitmap.
🡪 bitmap indexes are should not be used for tables involved in online transactions processing applications due to the internet mechanisms.
🡪 the column contains repeated values have then only created the bitmap index
51) what are the clusters ,can you explain briefly ?
A) clusters are database objects , which is used to improve performance of join query while manipulating the master-details table.
🡪A cluster is made up of a group of tables that share the same data blocks. The tables are grouped together because they share common columns and are often used together.
🡪 A cluster can contain a maximum of 32 tables.
🡪 Specify one or more names of columns in the cluster key. You can specify up to 16 cluster key columns. These columns must correspond in both datatype and size to columns in each of the clustered tables, although they need not correspond in name.
Index clusters
you can create tables in the cluster. However, before any rows can be inserted into the clustered tables, a cluster index must be created.
In an indexed cluster, Oracle Database stores together rows having the same cluster key value. Each distinct cluster key value is stored only once in each data block,
52) what are hierarchical queries ?
A) If a table contains hierarchical data, then you can select rows in a hierarchical order using the hierarchical query clause.
Hierarchical_query_clause::=
START WITH specifies the root row(s) of the hierarchy.
CONNECT BY specifies the relationship between parent rows and child rows of the hierarchy. This clause contains a condition that defines a relationship. Prior : this clause can contain other conditions to further filter the rows selected by the query.
Level: level is pseudo column , which is automatically assigns numbers to each level in a tree. Syntax :
sql> select level, colname from <tbmame> <where condition> startwith condition connect by prior parent col= child colname;
53) tell me merge concept ? A) Merge is a dml stmts which is used to trnfer data from source table to target table when those table are same strctu.
🡪Generally to avoid the multiple insert and update stmts conditionally using on clause .
syntax :
merge into <tb1> t1 using <tb2> e on (condition) when matched then update set (condtion) ,(condtion), when not matched then values(columns………..)
55) what is synonym ?
A) Sysnonym is a database ob, which provides security on table name and owner name. i.e it hide the object name and another schema name.
55) what are publc and private synonym ?
A) Specify PUBLIC to create a public synonym. Public synonyms are accessible to all users. However each user must have appropriate privileges on the object in order to use the synonym.
If you omit this PUBLIC clause, then the synonym is private and is accessible only within its schema. A private synonym name must be unique in its schema.
56) what is sequence ,and we can create sequence with cycle ?
A) Sequence is a independent database object, which is used to generates sequence numbers automatically.
Syntax :
sql> create sequence (seq name) start with n increment n min value n max value n cycle/no cycle cache/no cache;
57) What is curval and nextval?
Curval returns current value of sequence section.
Nextval returns next value of the sequence but every time sequence should start with next value.
58)How many types of locks?
DB has 2 types of locks. They are rowlevel and table level.
Locking set of rows by using for update clause. This clause used in select statement only.
Table level locks: (3 types)
Shared mode : One User can lock the table at share mode and other do not perform any kind of DML Operation except select.
Share update mode: If one user lock the table still it will allow the other user are allowed to perform DML operations on the record except the record access to the user.
Exclusive mode: Locking one user in exclusive mode then it wont allow the other user to perform any kind of operation.
Note: If 1user lock the table it wont allow the other user to lock the same table.
Wait and no wait: Wheneva we lock the any specific record in a table, we need to give wait or no wait option along with lock operation.
---Analytical Functions(8I features)---
59) What is the diff between rank and dense rank?
Rank : Wenever we use rank on particular column, It jus skips the next consecutive number i.e if it has same value allocate same number.
Dense rank: It do not skip the consecutive number i.e even it has same values allocate same to it and allocate next number to next value as per sequence.
60) What is virtual column?
Virtual column is a table which is used to store expression directly into a table column.
Syntax:
sql> Create table<table.name>(col1 data type,col2 datatype generated always as (col1+col2) virtual);
61) Diff betwn rownum and ROWID?
Rownum: ROWNUM is temporary value. ROWNUM only has meaning within the context of a single SQL statement, a way of referencing rows within a fetched result set.
ROWID: ROWID is permanent value. It is used to fetch a row . it is a physical address of rowids.
62) Diff between rollup and cube?
Rollup: It is used with group by clause to display the summarized date. ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns
Cube: CUBE generates a result set that shows aggregates for all combinations of values in the selected columns.
63)Diff btwn Materialized view and snap shot?
64) What is a partition?
Partition tables are used to improve the performance of applications in backup and recovery process. This is used to data warehouse applications. These tables are created based on key column.
Partitions are 3types: 1. Range 2.list 3.hash
When to Partition a Table??
Tables greater than 2 GB should always be considered as candidates for partitioning.
Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month’s data is updatable and the other 11 months are read only.
When the contents of a table need to be distributed across different types of storage devices.
65)What are the diff. b/w range ,list ,hash ?
Definition: A table that is partitioned by range is partitioned in such a way that each partition contains rows for which the partitioning expression value lies within a given range.
Creating range partitioned table
Definition: List partitioning enables you to explicitly control how rows map to partitions by specifying a list of discrete values for the partitioning key in the description for each partition.
Creating list partitioned table.
Definition: Hash partitioning maps data to partitions based on a hashing algorithm that Oracle applies to the partitioning key that you identify.
Creating hash partitioned table.
65) what is pivot and unpivote ? give syntax :
A) it 11g features, which is used to convert rows as columns and also display aggregate functional value in tabular form .
This function gives high performance compare to decode conversion function.
Sql> select * from (select col1…. from <tbname>)pivot(<group functions> for <col name> in(values...))
Topnth salaries
Sql> select level, max(sal) from emp where level=&level connected by prior sal > sal group by level;
Sql>select rownum,empno,ename,sal from (select * from emp order by sal desc) group by rownum,empno,ename,sal having rownum=’&n’;
Sql> with nth as (select ename,sal,dense_rank()over(order by sal desc ) as r1 from emp) select ename,sal from nth where r1=&n;
Sql> select sal from (select sal,dense_rank()over (order by sal desc) as topnth from emp)where topnth=&n;
Sql> select sal,ename from (select sal,dense_rank()over(order by sal desc) as r_num from emp) where r_num=&n;
Sql> select * from emp where sal in(select unique sal from emp order by sal desc fetch first 3 rows only);
65.1) write query return name from given mail?
Sql> select substr(‘siva@gmail.com’,1,instr(‘siva@gmail.com’,@’,2,1)-1) from dual;
65.2) return 2 and 5 rows in table?
Sql> select * from (select rownum r , emp.* from emp) where r in(2,5) or r< (select max(rownum-5) from emp);
or(last 4 records)
Sql> select * from (select rownum r ,emp.* from emp where rownum <=(select max(rownum) from emp)) where r>10;
66) what is funct ional index ? what is uses?
A) A function-based index allows you to match any WHERE clause in an SQL statement . 🡪remove unnecessary large-table full-table scans with super-fast index range scans. 🡪When you create a functional index, the database server computes the values of the user-defined function and stores them as key values in the index.
You can use a functional index for functions that return values of both user-defined data types (opaque and distinct) and built-in data types. However, you cannot define a functional index if the function returns a simple-large-object data type (TEXT or BYTE).
The benefits of using a function-based index includes:
function-based indexes are easy to create
An function-based index can compute complex values without using a trigger
A single unction-based index can change hundreds of SQL statements plans
A function-based-index can be built on an arithmetic expression or expression containing
PL/SQL, package functions, C callout or SQL built-in functions
The SQL optimizer can estimate selectivity more accurately if the expressions are materialized in a function-based index.
🡪CREATE INDEX idx_person_upper_given_names ON person (UPPER(given_names));
🡪CREATE INDEX idx_person_upper_last_name ON person (UPPER(last_name));
67) what id gtt?
A) Temporary tables exist only during the connection that declared them and cannot be referenced outside of that connection. When the connection closes, the rows of the table are deleted, and the in-memory description of the temporary table is dropped. 🡪ON COMMIT DELETE ROWS will delete the rows in the table Tcl command are used. 🡪 ON COMMIT PRESERVE ROWS The data will be preserve till the session is exists.
Temporary tables are useful when:
The table structure is not known before using an application.
Other users do not need the same table structure.
Data in the temporary table is needed while using the application.
The table can be declared and dropped without holding the locks on the system catalog.
Syntax
DECLARE GLOBAL TEMPORARY TABLE table-Name
{ column-definition [ , column-definition ] * }
[ ON COMMIT {DELETE | PRESERVE} ROWS ]
NOT LOGGED [ON ROLLBACK DELETE ROWS]
68) what is cardinality ?
A) Cardinality refers to a column contains uniqueness of data values.
🡪High cardinality means that the column contains a large percentage of totally unique values.
🡪 Low cardinality means that the column contains a lot of “repeats” in its data range.
Cardinality also sometimes refers to the relationships between tables. Cardinality between tables can be one-to-one, many-to-one or many-to-many.
69) what data dictionary of comments ?
A) all_col_comments , all_tab_comments , user_col_comments , user_tab_comments.
70) write query return ‘ - ’ in place of null values?
Sql> select ename,empno, sal ,nvl(to_char(comm),’-’) from emp;
71) write query to return “supreme authority” in null place of mgr column at name of king ?
Sql> select empno,ename, sal,job , nvl(to_char(mgr),’suprime authority’) from emp group by sal desc.
72) objects vs data data dictionaries?
Objects data dictionary
Table 🡪 dba_table or user_tables or all_tables.
Views 🡪 user_view.
Mviews 🡪 user_mviews.
Constraints 🡪 user_constraints or user_cons_columns.
Index 🡪 user_indexes.
Sequence 🡪 user_sequences.
Columns 🡪 user_tab_columns.
Cluster 🡪 user_clusters.
Synonym 🡪 USER_SYNONYMS.
Partitions 🡪 user_tab_partitions.
73) what is cache?
A) Cache is a memory area which is used to access sequence value very fast.
🡪 cache memory area store set of sequence numbers which is used to improves performance of the sequence. It is optional clause.
The default max value is 20 and default min value 2.
74) what is cycle and no cycle ?
A) When you create a sequence with CYCLE option, you are saying that when the sequence reaches its MAXVALUE, it will start over at the MINVALUE. This is not wise if using the sequence for primary key creation.
When you create a sequence with NOCYCLE option, you are saying that when the sequence reaches its MAXVALUE, it will NOT start over at the MINVALUE. When sequence reach max value oracle thrown error.
74) what are the EF CODD rules ?
1) Information Rule : all the information in a database should be stored in a table.
2) Guaranteed Access Rule : Each and every data value must be accessible without any ambiguity.
3) Comprehensive Data Sublanguage Rule : hould be at least one query language that enables the user to perform different functionalities within the database.
Almost all the DBMS and RDBMS applications use one major query language which is Standard Query Language (SQL).
4)View Updating Rule : all theoretically updatable views must be updatable in table.
5) Integrity Independence Rule :
6) High-level Insert, Update and Delete Rule: all dml operations are worked .
75) what are diff b/w nvl() and coalesce() function ?
76) howmany subquery you can written ?
A) Total 255 sub queries are written.
77) write a query for first name,mn ,last name from given string by using substr function?
Sql> select substr(name, 0, instr(name, ' ')-1) as first_name ,
substr(name, instr(name, ' ')+1,instr(name, ' ', -1, 1) - instr(name, ' ') - 1) as mid_name ,
substr(name, instr(name, ' ', -1) + 1) last_name from temp_1;
78) what is co- related sub query ?
A) Generally non-co related sub query execute first child query then after parent query executed.
But co-related sub query execute first parent query after then execute child query.
Co-related sub query child query is executed for each row per parent query table.
Must alias name are used in co-related sub query.
Syntax
Select * from table-name <alias-name > where column-name=(select * from table-name where column-name = aliasname . column -name);
79) why looking for job change?
A) “At my current job, I’ve learned skills that I’m ready to bring to the next level. I believe I can make a difference here.” or
a better opportunity, more challenges, and career growth.
I want to change my current job to get opportunity it term
of
new challenge,
extra responsibility,
new environment,
better technical future &
more scope of my professional growth to wide up my
experience meet the changing world.
80) what is diff b/w round and trunc ?
ROUND function used to round the number to the nearest while TRUNC used to truncate/delete the number from some position. Some cases both returns same result.
The ROUND() is used to round a numeric field to the nearest number of decimals specified.
e.g. 56 is nearer to 60 than 50 hence it will return 60.
e.g. If instead of 56 you use 54 then 54 is nearer to 50 and it will return 50.
TRUNC() returns a number truncated to a certain number of decimal places. So it always takes the lesser value.
e.g. If you take the number less than 56 it is 60 so it returns 50.
e.g. Even if you use 54 it will return 50 only.
81) how to give debug privegles?
SYS>grant create session,create procedure to scott;
SYS> grant debug connect session to scott;
SYS>grant debug any procedure to scott;
SYS>grant execute on DBMS_DEBUG_JDWP to scott;
82) WHAT IS TRIGGER ,GIVE SYNTAX?
A) A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed.
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;

Comments
Post a Comment