Answered Questions

  • how can we avoid duplicate rows. without using distinct command

    Pritty

    • Feb 29th, 2016

    The GROUP BY keyword helps to retrieve the unique values without using DISTINCT keyword.

    Malyadri M

    • Jun 22nd, 2015

    Code
    1. SELECT ROWID, E.*
    2. FROM EMP E
    3. WHERE ROWID = ( SELECT MAX(ROWID)
    4.                            FROM EMP1 E1
    5.                            WHERE E.ENO = E1.ENO
    6.                         );

  • Difference between a "where" clause and a "having" clause?

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: Ankush Sharma

    • Sep 14th, 2011


    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. The HAVING 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.

    for Example: if for an "Select" statement we use the "where" clause then the the result based on the "where" condition results and then we can use "group by" clause to arrange in some order, Now if we want to impose the condition on that group then we use "having" clause.

    The main reason for using WHERE clause is to select rows that are to be included in the query. For example, assume table Test.Suppose I want the names, account numbers, and balance due of all customers from California and Los Angles. Since STATE is one of the fields in the record format, I can use WHERE to select those customers.

    Using the code

    Code
    1. SELECT cusnum, lstnam, init
    2. FROM Test
    3. WHERE state IN ('CA', 'LA')
    4.  
    5. CUSNUM LSTNAM INIT BALDUE
    6. ====== ============ ==== ========
    7. 938472 John G K 37.00
    8. 938485 Mark J A 3987.50
    9. 593029 Lily E D 25.00
    Suppose I want the total amount due from customers by state. In that case, I would need to use the GROUP BY clause to build an aggregate query.
    Code
    1. SELECT state,SUM(baldue)
    2. FROM Test
    3. GROUP BY state
    4. ORDER BY state
    5.  
    6. State Sum(Baldue)
    7. ===== ===========
    8. CA 250.00
    9. CO 58.75
    10. GA 3987.50
    11. MN 510.00
    12. NY 589.50
    13. TX 62.00
    14. VT 439.00
    15. WY .00
    Using Having
    Code
    1. SELECT state,SUM(baldue)
    2. FROM Test
    3. GROUP BY state
    4. HAVING SUM(baldue) > 250
    5.  
    6.  
    7. State Sum(Baldue)
    8. ===== ===========
    9. GA 3987.50
    10. MN 510.00
    11. NY 589.50
    12. VT 439.00

    Ankush Sharma

    • Sep 14th, 2011

    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 ...

    Ankush Sharma

    • Sep 14th, 2011

    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 ...

  • In Oracle varchar2 takes dynamic space for storage then why char is still in oracle?

    Vijay Shewale

    • Sep 11th, 2014

    The reason behind char is still in oracle that is some of the table and objects are stored in oracle in char data types. to support that data types for that it is still in oracle. one more thing that ...

    Paras

    • Feb 23rd, 2013

    To Provide backward compatibility. i.e. program written in older version of Oracle might have CHAR used in it. The newer version should be compatible with it. Thats why CHAR is still there in Oracle ..

  • what is the difference between primary key, unique key, surrogate key?

    Star Read Best Answer

    Editorial / Best Answer

    Kolta Sam  

    • Member Since Jul-2011 | Jul 10th, 2011


    Primary Key:

    It is a visible key
    It generated by user or application.
    It could be changed by the user or application.
    It could be queried
    It used to form a relation between tables
    It shouldn’t contain null value
    It resemble table row
    It is a unique identifier for a table object.
    It contains only one key
    It could contain numeric and strings characters.
    It is an unique key which each row contain a distinct different key.
    Example for it is a customer_Id.
    It always starts by number one and second is two and so on but can starts with a different number.
    Could created on one or more columns
    No duplicate records

    Secondary Key:

    It used to form a relation between tables.
    It is alternate table key.
    It used to search data with primary key
    It could contains null value
    It could contains more than one secondary key for each table
    Created only on one columns
    No duplicate records
    It creates index clustered by default


    Surrogate Key:

    It is invisible key for the user or the application.
    It resembles database entity.
    It generated by the system so it is invisible for user and application.
    It shouldn’t contain null values
    Only one surrogate key for each data entity
    Its value is unique system wide.
    Its value is never manipulated by the user or the application.
    It never reused
    It is frequently sequential number
    It called synthetic key, an entity identifier, a system-generated key, a database sequence number, a factless key, a technical key, or an arbitrary unique identifier
    No duplicate records

    gopi

    • May 18th, 2018

    Primary key not allow null values where as unique allows but both are alternates.
    surrogate key serve as primery key in type 2 dimension tables

    Sukdya

    • Nov 27th, 2017

    Primary key doest not allow null values but unique key allows only one null value.