updated sql q&a

Upload: bnarendrababu

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 updated sql Q&A

    1/18

    SQL QUESTIONS AND ANSWERS

    1. What is the difference between TRUNCATE and DELETE commands?

    2. Which system table contains information on constraints on all the tables

    created?3. Explain normalization?

    4. Types of SQL functions

    5. WHAT OPERATOR PERFORMS PATTERN MATCHING?

    6. What is database?

    7. How can i hide a particular table name of our schema?

    8. How do I eliminate the duplicate rows?

    9. How do I display row number with records?

    10. Find out nth highest salary from EMP table11. Display Odd/ Even number of records

    12. WHAT IS A INLINE VIEW

    13. Which date function returns number value?

    14. What are the more common pseudo-columns?

    15. What is a relational database management system?

    16. State the difference between a primary key and foreign key?

    17. What is a synonym?

    18. What is a view?

    19. What is a schema?

    20. What is a join, explain the types of joins?

    21. What command is used to get back the privileges offered by the

    GRANT command?

    22. What command is used to create a table by copying the structure of

    another table?

    23. How can variables be passed to a SQL routine?

    24. What is the use of the DROP option in the ALTER TABLE command?

    25. What is a Cartesian product?

    26. What is the usage of SAVEPOINTS, COMMIT and ROLLBACK?

    27. What is difference between CHAR and VARCHAR2? What is the

    maximum SIZE allowed for each type?

    28. How many LONG columns are allowed in a table? Is it possible to use

    LONG columns in WHERE clause or ORDER BY?

    29. Can a view be updated/inserted/deleted? If Yes under what

    conditions?

    30. What are the advantages of VIEW?31. How can I find the total number of records in a table?

    1

  • 7/30/2019 updated sql Q&A

    2/18

    32. How can you compare a part of the name rather than the entire name?

    33. What is GROUP BY?

    34. How can I change my Oracle password?

    **************************************ANSWERS************************

    ***********

    1. What is the difference between TRUNCATE and DELETE

    commands?

    TRUNCATE DELETEIt is a DDL statement It is DML statement

    Truncate command will remove all

    the rows from a table, leaving the

    table empty.

    Using Delete command we can

    remove specific rows from a table

    using where clause.Once table is truncated the data is

    lost and data cannot be rollback.

    Data can be rollback before issuing

    COMMITIt is an auto commit statement. It is not an auto commit statement.

    In case of TRUNCATE Trigger doesn't

    get fired

    But in DML commands like DELETE.

    Trigger get firedIt free up the memory space

    Truncate is faster than delete

    SYNTAX SYNTAX

    TRUNCATE TABLE EMPLOYEES; DELETE FROM EMPLOYEESWHERE EMPLOYEE_ID = 100;

    2. Which system table contains information on constraints on all

    the tables created?

    USER_CONSTRAINTS table contains the information on

    constraints created on all the tables.

    SYNTAX

    SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME

    FROM USER_CONSTRAINTS

    WHERE TABLE_NAME='EMPLOYEES';

    OR

    SELECT * FROM USER_CONSTRAINTS;

    3. Difference between decode and case?

    Decode

    It specify the single column

    Can compare only discrete values

    Cannot process null

    2

  • 7/30/2019 updated sql Q&A

    3/18

    Case

    It specify the multiple columns

    Can handle range values (between, )

    Processing time is faster when compared to Decode Can process null

    All the expressions (columns) must be of the same date

    types.

    4. Types of functions

    Types of functions

    Single row functions

    Multiple row functions

    I.Case manipulation functions 1. Group

    Functions

    II.character manipulation functions

    III. Date functions

    IV. Data type conversion

    V.Number Functions

    VI. General Functions

    Single row functions

    CASE MANIPULATION FUNCTIONS

    Lower - select lower(last_name) from employees; O/P

    - king

    Upper - select upper(last_name) from employees; O/P

    - KING

    Inticap - select inticatp(last_name) from employees;

    O/P King

    CHARACTER MANIPULATION FUNCTIONS

    SUBSTR - select substr(last_name,1,3) from employees;

    3

  • 7/30/2019 updated sql Q&A

    4/18

    INSTR - select instr(last_name,n) from employees;

    LENGTH -select length(last_name) from employees;

    CONCAT - select concat(first_name,last_name) from

    employees; (only 2 arguement)

    REPLACE - select replace (last_name,a,123) from employees;

    (a replace with 123)

    REVERSE - select reverse(last_name) from employees;

    LPAD / RPAD - select lpad(last_name,20,#) from

    employees;

    LTRIM / RTRIM - select ltrim(last_name,k) from employees;

    DATE FUNCTIONS

    MONTHS_BETWEEN - select months_between(sysdate,01-jan-

    10) from dual;

    (only months_between date functions returns anumber)

    ADD_MONTHS - select add_months(sysdate,3) from dual;

    NEXT_DAY - select next_day(sysdate,Friday) from dual;

    LAST_DAY - select last_day (sysdate) from dual;

    DATA TYPE CONVERSION

    TO-DATE

    TO-CHAR

    TO_NUMBER

    4

  • 7/30/2019 updated sql Q&A

    5/18

    NUMBER FUNCTIONS

    ROUND

    TRUNC

    MOD

    General functions

    NVL - it accepts 2 arguments. If the 1st argument is null

    it return 2nd arguments value. Else it returns the 1st

    arguments value.NVL2 - it accepts 3 arguments. 1st argument is null it

    return 3rd arguments value. Else it returns the 2nd

    arguments value.

    NULLIF - it accepts 2 arguments. If the both arguments

    are equal it returns null. Else it returns the 1st arguments

    value.COALESCE - it accepts N number of arguments. It

    returns the 1st not null value.

    Multiple row functions

    Group functions

    MAX

    MIN

    COUNT

    SUM

    AVG

    5

  • 7/30/2019 updated sql Q&A

    6/18

    5. WHAT OPERATOR PERFORMS PATTERN MATCHING? We have to use the LIKE operators in the where clause to perform

    the pattern matching.

    SYNTAX

    1.For finding first character pattern matching :-

    Select *

    From employees

    Where last_name likea%;

    2. For finding last character pattern matching :-

    Select *

    From employees

    Where last_name like%a;

    3.For finding the letter in any part of the last_name:-

    Select *

    From employees

    Where last_name like%a%

    6. What is database?

    A database is the collection of information where we can store the

    date, manipulate the date and retrieve the stored data.

    Database is designed so that we can store and manage the

    enterprise information

    7. How can I hide a particular table name of our schema

    Using SYNONYMNS we can hide a particular table name of our

    schema.

    SYNTAX

    CREATE SYNONYMN EMP for EMPLOYEES;

    After creating the above synonymn we can access the data of EMPLOYEES

    table using EMP as table name as below

    6

  • 7/30/2019 updated sql Q&A

    7/18

    SELECT * from EMP;

    We can also give alternate name to the objects as well.

    8. How do I eliminate the duplicate rows ?

    Using DISTINCT keyword we can eliminate duplicate records.

    SYNTAX

    SELECT DISTINCT DEPARTMENT_ID

    FROM EMPLOYEES;

    9.How do I display row number with records?

    Using ROWNUM keyword we can display row number with records.

    Rownum is a pseudocolumn.

    After issuing a selectstatement, oracle does is to assign an increasing

    (starting with 1, increased by 1) number to each row returned.

    SYNTAX

    SELECT ROWNUM,EMPLOYEE_ID

    FROM EMPLOYEES;

    10.Find out nth highest salary from emp table

    SYNTAX

    select salary from

    (select salary,rownum RK from

    (select salary from employees

    order by salary desc))

    where RK=n;

    11.Display Odd/ Even number of records

    We can use the MOD functions is number functions

    SYNTAX

    ODD:

    select * from employees

    where (rowid,1) in

    (select rowid, mod(rownum,2) from employees)

    Alternative query

    select * from employees

    where mod(employee_id,2)=0;

    EVEN:

    select * from employees

    7

  • 7/30/2019 updated sql Q&A

    8/18

    where (rowid,0) in

    (select rowid, mod(rownum,2) from employees)

    Alternative query

    select * from employees

    where mod(employee_id,2)=1;

    12. WHAT IS A INLINE VIEW

    An inline view is a SELECT statement in the FROM-clause of another

    SELECT statement.

    In-line views are commonly used simplify complex queries by removing

    join operations and condensing several separate queries into a singlequery.

    SYNTAX

    SELECT SALARY FROM

    (SELECT SALARY , ROWNUN RK FROM

    (SELECT SALARY FROM EMPLOYEES ORDER BY SALARY DESC) )

    WHERE RK=4

    13. Which date function returns number value?

    MONTHS_BETWEEN date function returns number value.

    In that date function, we can able to find the months between the defined

    date by the user.

    SYNTAX

    select months_between (sysdate,'01-jan-10') from dual;

    date1 date2

    The date1 is later than date2 Result will be in Positive.The date1 is earlier than date2 Result will be in Negative.

    The defined date format should be in DD-MM-YY. If we insert the other

    date format, the oracle server will through the error.

    14. What are the more common pseudo-columns?

    A. ROWNUM

    Rownum is a pseudo column. It numbers the records in a result set.

    The first record that meets the where criteria in a select statement is

    8

  • 7/30/2019 updated sql Q&A

    9/18

    given rownum=1, and every subsequent record meeting that same criteria

    increases rownum.

    B. ROWID

    A rowid is a pseudo column (like versions_xid), that uniquely identifies a

    row within a table, but not within a database. It is possible for two rows of

    two different tables stored in the same cluster to have the same rowid.

    C.SYSDATE

    Sysdate is a pseudo column, Itll show the system date.

    D.SYSTIMESTAMP

    Systimestamp is a pseudo column, Itll show the system date with time

    E.USER

    USER is a pseudo column. It returns the name of the user currentlyconnected

    D.UID

    UID is a pseudo column,itll display the user ID. For every user the oracle

    server has produces the unique ID for every user.

    15. What is a relational database management system?

    RDBMS

    A database management software system that organizes data into a

    series of records that are stored in linked tables. This provides the ability

    to relate different records, fields and tables, and aids data access and data

    transformation.

    A relational Databse is collections of relation (or) two

    dimensional tables. The organization wants to store the information

    about all the employees in your company. In the relational

    databases the informational can store in the different tables. All the

    table have relations

    16. State the difference between a primary key and foreign key?

    PRIMARY KEY FOREIGN KEY

    Uniquely identifies each row of the table It is referencial identifier

    Itll not allow the null values Itll allows the null values

    9

  • 7/30/2019 updated sql Q&A

    10/18

    Itll not allow the duplicate values Itll allows the duplicate values

    Only one primary key allow in a table Each table can have more than one

    foreign keyPrimary key is unique foreign key reference as Primary key in

    another table

    Oracle server enforce the uniqueness by

    creating a unique index on the primary

    key column

    Oracle server doesnt enforce such

    unique index for the foreign key

    column.

    17. What is a synonym?

    SYNONYM is an alternative name for an object. The

    advantages are

    a. creating easy reference to a table.

    b. shorter length object name.

    SYNTAX

    To create synonym

    CREATE SYNONYM SEMP for EMPLOYEES;

    To drop synonymDrop synonym SEMP;

    To create the public synonym

    Create public synonym dept for deparments;

    The public synonym is created so that the all users can

    access the synonyms. The database administrator can create

    the public synonym. And the removal of public synonym can

    be done only by the database administrator.

    Drop public synonym dept;

    To display all the synonyms created by the user

    Select * from user_synonyms;

    18. What is a view?

    A VIEW is a virtual table.

    View logicaly a subset of data from two or more table.

    Views contains no data of its own

    Its a window through which the data from table can view.Types of Views VIEWS

    10

  • 7/30/2019 updated sql Q&A

    11/18

    SIMPLE VIEWS COMPLEX VIEWSONE Number of tables One or moreNO Contains Functions YESNO Contains groups of

    data

    YES

    YES DML operations

    through view

    Not always

    SYNTAX

    To create the view

    Create or replace view [View_name]

    AS

    Select statement

    To drop the view

    Drop view [view_name]

    WITH READ ONLY keyword

    Create or replace view [View_name]

    AS

    Select statement

    With read only

    By adding with read only keyword, the DML operators are

    restricted in the particular view.

    Advantages

    It is used to restrict data access

    It makes complex query easy

    Views can hide the complexity of data; for example a view could appear as

    Sales2000 or Sales2001, transparently partitioning the actual underlying

    table

    Views take very little space to store; the database contains only the

    definition of a view, not a copy of all the data it presents.

    19. What is a schema?

    A SCHEMA is a collection of database objects.

    A schema is owned by a database user and has the same name as

    that user.

    Schema objects are logical structures created by users to contain,or reference, their data.

    11

  • 7/30/2019 updated sql Q&A

    12/18

    Schema objects include structures like tables, views, and indexes.

    20. What is a join, explain the types of joins?

    Selecting data from 2 or more tables is called as joins.

    To link the two table, first we have to find out the column

    which is exist in the both the table

    Types of joins

    1. Natural joins

    2. Cross joins

    3. Outer joins

    4. Equi joins

    5. Nonequijoins

    6. Self joins

    NATURAL JOINS

    Natural joins clause is based on all columns in the two tables

    that have the same name.

    The selected rows frim the two tables that have equal values

    in all matched column.

    The natural joins automatically select the column which is

    common in the both tables. But the column name in the both

    table should be the same and data types as well

    SYNTAX

    Select column_name1,column_name2

    From table1

    NATURAL JOIN table2

    CROSS JOINS

    The cross join clause produces the cross product of two

    tables.

    SYNTAX

    Select column_name1,column_name2

    From table1

    CROSS JOIN table2

    OUTER JOINS

    Selecting matched, Un-matched records is called as outerjoins.

    12

  • 7/30/2019 updated sql Q&A

    13/18

    Types of outer joins

    1. left outer joins

    2. Right outer joins

    3. Full outer joins

    Left Outer Joins

    A joins between two tables that returns the matched rows as

    well as the unmatched rows from the left table is called as

    left outer join

    SYNTAX

    select e.department_id,

    d.department_name,

    from departments d left outer join employees eon (e.department_id = d.department_id);

    Right Outer Joins

    A joins between two tables that returns the matched rows as

    well as the unmatched rows from the Right table is called as

    Right outer join

    SYNTAX

    select e.department_id,

    d.department_name,

    from departments d Right outer join employees e

    on (e.department_id = d.department_id);

    Full Outer joins

    A join between that returns the matched rows and the

    unmatched rows from the both left and right tables is called

    as full outer joins.

    SYNTAX

    select e.department_id,

    d.department_name,

    from departments d full outer join employees e

    on (e.department_id = d.department_id);

    21. What command is used to get back the privileges offered by

    the GRANT command?

    REVOKE command is used to get back the privileges offered

    by the GRANT command.

    13

  • 7/30/2019 updated sql Q&A

    14/18

    The SQL command revoke allows to take away System

    privileges and object privileges from users and roles

    22. What command is used to create a table by copying the

    structure of another table?

    SYNTAX

    CREATE TABLE EMP AS

    SELECT * FROM EMPLOYEES

    WHERE 1=2 Invalid Condition

    If we doesnt give the invalid condition in the where clause

    the whole data will copy to the new table (EMP table). Forcopying the structure alone we have to give one invalid

    condtion in the where clause.

    23. How can variables be passed to a SQL routine?

    using the & symbol a variables can be passed to a SQL

    routine.

    It is a SQLPLUS command.

    Entering the data in a row

    SYNTAX

    Insert into t1 values(&a,&b);

    We can assign the both the values every time.

    Insert into t1 values (&&a,&b);

    We can fix the a value as constant and b value will change

    24. What is the use of the DROP option in the ALTER TABLE

    command?

    The use of the DROP option in the ALTER TABLE command is to dropa specific COLUMN.

    SYNTAX

    ALTER TABLE TABLE_NAME

    DROP COLUMN COLUMN_NAME

    25. What is a Cartesian product?

    A Cartesian product is a result set which contains all the possible

    combinations of each row in each table included in a query.

    A Cartesian product is almost always an incorrect result.

    14

  • 7/30/2019 updated sql Q&A

    15/18

    When a join condition is invalid or omitted completely the result is

    known as Cartesian product.

    By using the cross join , the Cartesian product will produce.

    To avoid the Cartesian product, insert the valid join condition.

    26. What is the usage of SAVEPOINTS, COMMIT, ROLLBACK?

    COMMIT is used to confirm the changes done whenever a

    DML(INSERT/UPDATE/DELETE) statement is issued on a table.

    SYNTAX

    COMMIT;

    ROLLBACK is used to undo the changes done issued by a

    DML(INSERT/UPDATE/DELETE) statement command.

    SYNTAX

    ROLLBACK;

    Note:- we can undo the changes unless a COMMIT is not issued.

    SAVEPOINT

    We specify the savepoint after the current transaction with specified

    name.

    We can rollback to that particular point by rollback to

    savepoint_name

    Example:-

    Insert1

    Update1

    Savepoint A First savepoint

    Insert 2Update2

    Savepoint B second savepoint

    If we gave the rollback command as

    ROLLBACK TO A;

    The rollback operators execute upto the savepoint A. The

    update2 and insert2 will not be committed until we gave

    commit command.

    15

  • 7/30/2019 updated sql Q&A

    16/18

    27. What is difference between CHAR and VARCHAR2? What is

    the maximum SIZE allowed for each type?

    CHAR VARCHARThe char is a fixed-length character

    data type.

    The varchar is a variable-length

    character data type.The storage size of the char value is

    equal to the maximum size for this

    column.

    The storage size of the varchar

    value is the actual length of the

    data entered, not the maximum size

    for this column.A char can be used when the data

    entries in a column are expected to

    be the same size.

    A varchar can be used when the

    data entries in a column are

    expected to vary considerably in

    size.Maximum size is 2000 and

    (minimum/default) is 1

    Maximum size is 4000 and

    (minimum/default) is 1

    28. How many LONG columns are allowed in a table? Is it possible

    to use LONG columns in WHERE clause or ORDER BY?

    Only One LONG column is allowed in each table.

    Long column cannot be included in group by clause (or) order by

    clause.

    Long column is not copied when a table is created using sub-query.

    No constraints can be defined on a long column.

    29. Can a view be updated/inserted/deleted? If Yes under what

    conditions?

    You cannot add the data through a view, if the view contains the

    following.

    Group Functions

    Group by Clause

    DISTINCT Keyword

    Pseudo column ROWNUM keyword

    Columns defined by expressions

    NOT NULL column in the base table that are not selected by view.

    30. What are the advantages of VIEW?

    16

  • 7/30/2019 updated sql Q&A

    17/18

    To restrict data access

    To make complex queries easy

    To provide data independence

    To present different views of same data

    31. How can I find the total number of records in a table?

    To find the number of records in a table we can use the

    COUNT(*) function.

    SYNTAX

    SELECT COUNT(*) FROM EMPLOYEES;

    32. How can you compare a part of the name rather than the

    entire name?

    By using LIKE operator in the where clause we can compare

    the part of the name.

    Example:-

    Select employee_id,last_name from employees

    Where last_name like %A;

    33.What is GROUP BY?

    The GROUP BY statement is used in conjunction with the

    aggregate functions to group the result-set by one or more

    columns.

    The GROUP BY clause can be used in a SELECT statement to

    collect data across multiple records and group the results byone or more columns.

    If the arguement / Expression is added in the select

    statement with the group function. That expression will

    include with the group by clause in the statement.

    If you fail to include the expression in the GROUP BY clause,

    the ORACLE server through the error.

    The GROUP BY column doesnt have to be in the select list.

    17

  • 7/30/2019 updated sql Q&A

    18/18

    SYNTAX

    SELECT EMPLOYEE_ID,FIRST_NAME,MAX(SALARY) FROM EMPLOYEES

    WHERE SALARY >15000

    GROUP BY EMPLOYEE_ID,FIRST_NAME;

    34. How can I change my Oracle password?

    ALTER user hr identified by admin;

    18