Adbms File
Adbms File
Adbms File
Write the SQL To copy both the structure and data of Table-1 into Table-2, Table-1 and
Table-2 are given below?
Table-1 Store_Information
Store_Name
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Table-2 Geography
Region_Name
Store_Name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Q2. Write the SQL To copy the structure of Table-1 into Table-2 without any data .
Table-1 Store_Information
1
Store_Name Sales
Txn_Date
Jan-05-1999
San Diego
250
Jan-07-1999
Jan-08-1999
Boston
Jan-08-1999
700
Table-2 Geography
Region_Name Store_Name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Q3. We want to find out sales by store, and we only want to see stores with sales listed in the
report. Write the corresponding SQL query.
Table-A1 Store_Information
Store_Name Sales
Txn_Date
Jan-05-1999
San Diego
250
Jan-07-1999
Jan-08-1999
Boston
Jan-08-1999
700
Table-A2 Geography
Region_Name
Store_Name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Description
[charlist]
[^charlist] or [!
charlist]
Q5. if we want to get the lowest sales from the following table, Write the corresponding SQL
query.
Table- Store_Information
Store_Name
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Q6. Write the SQL statement is a Cartesian join between the Store_Information and
the Geography tables:
Table - Store_Information
Store_Name
Sales Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Table - Geography
Region_Name
Store_Name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Q7. We want to find out sales by store, and we want to see the results for all stores regardless
whether there is a sale in the Store_Information table. To do this, we can use the following SQL
statement using LEFT OUTER JOIN: Write tha SQL statement.
Table Store_Information
Store_Name
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Table Geography
Region_Name
Store_Name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Data Type
First_Name
char(50)
Last_Name
char(50)
Address
char(50)
City
char(50)
Country
char(25)
Birth_Date
datetime
Table Store_Information
Store_Name
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Table Internet_Sales
Txn_Date
Sales
Jan-07-1999
250
Jan-10-1999
535
Jan-11-1999
320
Jan-12-1999
750
Q10. Assume we have two tables: The first table is User_Address, which maps each user to a
ZIP code; the second table is User_Score, which records all the scores of each user. The question
is, how to write a SQL query to find the number of users who scored higher than 200 for each
ZIP code?
Ans. SELECT count(User_Name) FROM User_Address INNER JOIN User_Score ON
User_Adress.User_Name = User_Score.User_Name where User_Score > 200 GROUP BY
User_Zip;
Q11 . Let's assume that we have the following two tables, and we want to find out all the dates
where there are both store sales and internet sales. Write the SQL statement.
Table Store_Information
Store_Name
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
Los Angeles
300
Jan-08-1999
Boston
700
Jan-08-1999
Table Internet_Sales
Txn_Date
Sales
Jan-07-1999
250
Jan-10-1999
535
Jan-11-1999
320
Jan-12-1999
750
Sales
Txn_Date
Los Angeles
1500
Jan-05-1999
San Diego
250
Jan-07-1999
San Francisco
300
Jan-08-1999
Boston
700
Jan-08-1999
Q13.Write the following SQL query ?The query will be executed with specific problem and
corresponding result.
Q 1. Explain SQL INSTR?
8
Ans. The INSTR functions search string for substring. The function returns an integer
the position of the character in string that is the first character
occurrence. INSTR calculates strings using characters as defined by the input
set. INSTRB uses bytes
instead of characters. INSTRC uses Unicode
characters. INSTR2 uses UCS2 code points. INSTR4 uses UCS4 code points.
indicating
of this
character
complete
Ans.
The SUBSTR functions
return
a
portion
of char,
beginning
at
character position, substring_length characters long. SUBSTR calculates lengths using characters
as defined by the input character set. If position is 0, then it is treated as 1.
1. If position is positive, then Oracle Database counts from the beginning of char to find the
first character.
2. If position is negative, then Oracle counts backward from the end of char.
3. If substring_length is omitted, then Oracle returns all characters to the end of char.
If substring_length is less than 1, then Oracle returns null.
Examples
The following example returns several specified substrings of "ABCDEFG":
SELECT SUBSTR('ABCDEFG',3,4) "Substring"
FROM DUAL;
OUTPUT:
Substring
--------CDEF
Ans. LTRIM removes from the left end of char all of the characters contained in set. If you do
not specify set, it defaults to a single blank. If char is a character literal, then you must enclose it
in single quotes. Oracle Database begins scanning char from its first character and removes all
characters that appear in setuntil reaching a character not in set and then returns the result.
Examples
The following example trims the redundant first word from a group of product names in
the oe.products table:
SELECT product_name, LTRIM(product_name, 'Monitor ') "Short Name"
FROM products
WHERE product_name LIKE 'Monitor%';
OUTPUT:
PRODUCT_NAME
Short Name
--------------------
---------------
Monitor 17/HR
17/HR
Monitor 17/HR/F
17/HR/F
Monitor 17/SD
17/SD
Monitor 19/SD
19/SD
Ans.
REPLACE returns char with
every
occurrence
of search_string replaced
with replacement_string. If replacement_string is omitted or null, then all occurrences
of search_string are removed. If search_string is null, then char is returned.
Examples
The following example replaces occurrences of J with BL:
SELECT REPLACE('JACK and JUE','J','BL') "Changes"
FROM DUAL;
OUTPUT:
Changes
-------------BLACK and BLUE
Q8. Explain SQL TO DATE?
Ans. TO_DATE converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a
value of DATE datatype. The fmt is a datetime model format specifying the format of char. If
you omit fmt, then char must be in the default date format. If fmt is J, for Julian, then char must
be an integer.
Examples
The following example converts a character string into a date:
SELECT TO_DATE(
'January 15, 1989, 11:00 A.M.',
'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American')
FROM DUAL;
OUTPUT:
TO_DATE('
--------15-JAN-89
Ans. UNION returns the results of two queries minus the duplicate rows. The following two
tables represent the teams:
INPUT:
SQL> SELECT * FROM FOOTBALL;
OUTPUT:
NAME -------------------- ABLE BRAVO CHARLIE DECON EXITOR FUBAR GOOBER 7
rows selected.
13
Ans. The SQL INTERSECT clause/operator is used to combine two SELECT statements, but
returns rows only from the first SELECT statement that are identical to a row in the second
SELECT statement. This means INTERSECT returns only common rows returned by the two
SELECT statements.
Syntax:
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
INTERSECT
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Q12. Explain SQL DECODE?
Ans. DECODE compares expr to each search value one by one. If expr is equal to a search, then
Oracle Database returns the corresponding result. If no match is found, then Oracle
returns default. If default is omitted, then Oracle returns null.
The
maximum
number
of
components
including expr, searches, results, and default, is 255.
in
Examples
This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns
'Southlake'; ifwarehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is
not 1, 2, 3, or 4, then the function returns 'Non domestic'.
SELECT product_id,
DECODE (warehouse_id, 1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non domestic') "Location"
FROM inventories
WHERE product_id < 1775
ORDER BY product_id, "Location";
Ans. A subquery is a query whose results are passed as the argument for another query.
Subqueries enable you to bind several queries together.
NAME
Cat
Camel
Penguin
Lax
15
Ans. Returns the next value in a sequence. Calling NEXTVAL after creating a sequence
initializes the sequence with its default value, incrementing a positive value for ascending
sequences, and decrementing a negative value for descending sequences. Thereafter, calling
NEXTVAL increments the sequence value. NEXTVAL is used in INSERT, COPY, and SELECT
statements to create unique values.
Syntax
[[db-name.]schema.]sequence_name.NEXTVAL
NEXTVAL('[[db-name.]schema.]sequence_name')
Q18. Explain SQL NULL VALUES?
Ans. If a column in a table is optional, we can insert a new record or update an existing record
without adding a value to this column. This means that the field will be saved with a NULL
value.
SQL Working with NULL Values
Look at the following "Persons" table:
P_Id
LastName
FirstName
Hansen
Ola
Svendson
Tove
Pettersen
Kari
Address
City
Sandnes
Borgvn 23
Sandnes
Stavanger
Suppose that the "Address" column in the "Persons" table is optional. This means that if we
insert a record with no value for the "Address" column, the "Address" column will be saved with
a NULL value.
Q19. Explain SQL ISNULL(), IFNULL() ?
Ans. IS NULL() is used to return a Null Value.
SQL> SELECT *
2 FROM PRICE
3 WHERE WHOLESALE IS NULL;
OUTPUT:
ITEM WHOLESALE
--------------
----------
ORANGES
IS NULL() is used to Check whether a value is a Null Value.
Q20. Explain SQL INLINE VIEW?
16
Ans. An in-line view is a nested query that is specified in the FROM clause. An in-line view
selects data from one or more tables to produce a temporary in-memory table. This virtual table
exists only during the query. An in-line view is a SELECT statement within a SELECT
statement, which we call a nested statement. Nested SELECT statements sound complex but they
are not.
Q21. Explain SQL SEQUENCE?
Ans. Each sequence can be granted access control information, so that they can be referenced, or
altered and dropped. The sequence name is used to fetch the value of the sequence, using two
special suffixes, called pseudo columns, which either force the return of a new value
(NEXTVAL), or return the current value (CURRVAL) for the current session.
SELECT privilege on sequence
USAGE privilege on sequence schema
Examples
The following example creates an ascending sequence called my_seq, starting at 101:
CREATE SEQUENCE my_seq START 101;
The following command generates the first number in the sequence:
SELECT NEXTVAL('my_seq');
OUTPUT:
nextval
--------101
(1 row)
17
Ans. The SQL CASE expression normally is composed of case-operand, when-condition and
resultexpression. They must be valid sql-expressions. The syntax format goes like the following:
CASE<case-operand>
WHEN when condition THEN result-expression
<WHEN when-condition THEN result-expression>...
<ELSE result-expression>
END.
Ans. Minus returns the rows from the first query that were not present in the second. For
example:
SQL> SELECT * FROM FOOTBALL
2 MINUS
3 SELECT * FROM SOFTBALL;
OUTPUT:
NAME
-------------------BRAVO
DECON
Q26. Explain SQL EXISTS?
Ans. EXISTS takes a subquery as an argument and returns TRUE if the subquery returns
anything and FALSE if the result set is empty. For example:
INPUT/OUTPUT:
SELECT NAME, ORDEREDON
FROM ORDERS
WHERE EXISTS
(SELECT *
FROM ORDERS
WHERE NAME ='TRUE WHEEL')
NAME
ORDERED ON
==========
===========
TRUE WHEEL
15-MAY-1996
TRUE WHEEL
19-MAY-1996
TRUE WHEEL
2-SEP-1996
TRUE WHEEL
30-JUN-1996
BIKE SPEC
30-JUN-1996
19
20