Interview SQL and PL SQL

April 3, 2017 | Author: sreejtih | Category: N/A
Share Embed Donate


Short Description

Download Interview SQL and PL SQL...

Description

inner and outer join Inner join returns rows when there is at least one match in both tables Outer Join, on the other hand, will return matching rows from both tables as well as any unmatched rows from one or both the tables (based on whether it is single outer or full outer join respectively). Assuming you're joining on columns with no duplicates, which is a very common case:  An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.  An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union. Examples Suppose you have two tables, with a single column each, and data as follows: A 1 2 3 4

B 3 4 5 6

Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B. Inner join An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common. select * from a INNER JOIN b on a.a = b.b; select a.*,b.* from a,b where a.a = b.b; a | b --+-3 | 3 4 | 4 Left outer join A left outer join will give all rows in A, plus any common rows in B. select * from a LEFT OUTER JOIN b on a.a = b.b; select a.*,b.* from a,b where a.a = b.b(+); a | b --+----1 | null

2 | null 3 | 3 4 | 4 Full outer join A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa. select * from a FULL OUTER JOIN b on a.a = b.b; a | b -----+----1 | null 2 | null 3 | 3 4 | 4 null | 6 null | 5

Inner Join:

Full Outer Join:

What command is used to create a table by copying the structure of another table? CREATE TABLE.. AS SELECT command Explanation: To copy only the structure, the WHERE clause of the SELECT command should contain a false statement as in the following. CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;

What is the advantage of specifying WITH GRANT OPTION in the GRANT command? The privilege receiver can further grant the privileges he/she has obtained from the owner to any other user.

What is the main difference between the IN and EXISTS clause in sub-queries? The main difference between the IN and EXISTS predicate in sub-query is the way in which the query gets executed. IN -- The inner query is executed first and the list of values obtained as its result is used by the outer query. The inner query is executed for only once. EXISTS -- The first row from the outer query is selected, then the inner query is executed and , the outer query output uses this result for checking. This process of inner query execution repeats as many no. of times as there are outer query rows. That is, if there are ten rows that can result from outer query, the inner query is executed that many no. of times.

A column has some negative values and some positive values. It is required to find the sum of negative numbers and the sum of the positive numbers in two separate columns. 1 SELECT 2 SUM(CASE WHEN num < 0 THEN num ELSE 0 END) neg, 3 SUM(CASE WHEN num > 0 THEN num ELSE 0 END)pos 4 FROM neg_pos; How to search for strings containing ‘%’ in Oracle? Search for columns containing ‘%’ in Oracle. In ORACLE , you can use the ESCAPE keyword to search for strings containing ‘%’. Otherwise it would be considered as a META CHARACTER . Using the escape character ( to search for strings containing like ‘ABC %%TRF’, ‘TR%FF’ or ‘%GH’) Answer : 1 SELECT col_name FROM tbl_name WHERE col_name LIKE '%?%%' ESCAPE '?'; 2

Here ‘?’ can be replaced with any other character. 1 SELECT col_name FROM tbl_name 2 WHERE instr(col_name,'%') > 0 How does one remove special characters in ORACLE? To replace special characters in a string with null use translate : translate(‘string’,'to_replace’,'replace_with’) for eg: 1 SELECT translate 2 ('asdfsd@#@$#$%$sdfg&;','!@#$%^&;*()_+=-`~?>
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF