Question : View the Exhibits and examine products and sales tables. You issue the following query to display product name and the number of times the product has been sold: What happens when the above statement is executed? 1. The statement executes successfully and produces the required output. 2. The statement produces an error because item_cnt cannot be displayed in the outer query. 3. The statement produces an error because a subquery in the from clause and outer-joins cannot be used together. 4. The statement produces an error because the group by clause cannot be used in a subquery in the from clause.
Correct Answer : 4 Explanation: RIGHT OUTER JOIN operation, A RIGHT OUTER JOIN is one of the JOIN operations that allow you to specify a JOIN clause. It preserves the unmatched rows from the second (right) table, joining them with a NULL in the shape of the first (left) table. A LEFT OUTER JOIN B is equivalent to B RIGHT OUTER JOIN A, with the columns in a different order. Syntax TableExpression RIGHT [ OUTER ] JOIN TableExpression { ON booleanExpression | USING clause } The scope of expressions in the ON clause includes the current tables and any tables in query blocks outer to the current SELECT. The ON clause can reference tables not being joined and does not have to reference either of the tables being joined (though typically it does). Example 1 : -- get all countries and corresponding cities, including -- countries without any cities SELECT COUNTRIES.COUNTRY, CITIES.CITY_NAME FROM CITIES RIGHT OUTER JOIN COUNTRIES ON CITIES.COUNTRY_ISO_CODE = COUNTRIES.COUNTRY_ISO_CODE -- get all countries in Africa and corresponding cities, including -- countries without any cities
SELECT COUNTRIES.COUNTRY, CITIES.CITY_NAME FROM CITIES RIGHT OUTER JOIN COUNTRIES ON CITIES.COUNTRY_ISO_CODE = COUNTRIES.COUNTRY_ISO_CODE WHERE Countries.region = 'Africa' -- use the synonymous syntax, RIGHT JOIN, to achieve exactly -- the same results as in the example above
SELECT COUNTRIES.COUNTRY, CITIES.CITY_NAME FROM CITIES RIGHT JOIN COUNTRIES ON CITIES.COUNTRY_ISO_CODE = COUNTRIES.COUNTRY_ISO_CODE WHERE Countries.region = 'Africa' Example 2 -- a TableExpression can be a joinOperation. Therefore -- you can have multiple join operations in a FROM clause -- List every employee number and last name -- with the employee number and last name of their manager
SELECT E.EMPNO, E.LASTNAME, M.EMPNO, M.LASTNAME FROM EMPLOYEE E RIGHT OUTER JOIN DEPARTMENT RIGHT OUTER JOIN EMPLOYEE M ON MGRNO = M.EMPNO ON E.WORKDEPT = DEPTNO
Question : You want to create a table employees in which the values of columns EMPLOYEES_ID and LOGIN_ID must be unique and not null. Which two SQL statements would create the required table? 1. A,B 2. C,D 3. D,E 4. B,E 5. A,E
Correct Answer : 3 Exp: you can have NULLs in your columns unless the columns are specified NOT NULL. You will be able to store only one instance of NULLs however (no two sets of same columns will be allowed unless all columns are NULL) : SQL> CREATE TABLE t (id1 NUMBER, id2 NUMBER); SQL> ALTER TABLE t ADD CONSTRAINT u_t UNIQUE (id1, id2); SQL> INSERT INTO t VALUES (1, NULL); 1 row inserted SQL> INSERT INTO t VALUES (1, NULL); INSERT INTO t VALUES (1, NULL) ORA-00001: unique constraint (VNZ.U_T) violated SQL> /* you can insert two sets of NULL, NULL however */ SQL> INSERT INTO t VALUES (NULL, NULL); 1 row inserted SQL> INSERT INTO t VALUES (NULL, NULL); 1 row inserted An inline not null constraint is declared after each individual column name: CREATE TABLE test ( ... NAME VARCHAR2(64), PRODUCT_ID VARCHAR2(24) not null, ... ) If you wish to name the constraint explicitly then you also do this after each column: CREATE TABLE test ( ... NAME VARCHAR2(64), PRODUCT_ID VARCHAR2(24) CONSTRAINT PRODUCT_ID_NN NOT NULL, ... ) You can of course modify the column after the table has been created: ALTER TABLE test MODIFY product_id varchar2(24) not null;
Question : View the Exhibit and examine the structure of the products table. Using the products table, you issue the following query to generate the names, current list price, and discounted list price for all those products whose list price falls below $10 after a discount of 25% is applied on it. The query generates an error. What is the reason for the error?
1. The parenthesis should be added to enclose the entire expression. 2. The double quotation marks should be removed from the column alias. 3. The column alias should be replaced with the expression in the where clause. 4. The column alias should be put in uppercase and enclosed within double quotation marks in the where clause.
Correct Answer : 3
Explanation: An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column:
SELECT SQRT(a*b) AS root FROM tbl_name GROUP BY root HAVING root > 0; SELECT id, COUNT(*) AS cnt FROM tbl_name GROUP BY id HAVING cnt > 0; SELECT id AS 'Customer identity' FROM tbl_name; Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. For example, the following query is illegal:
SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id; The WHERE clause determines which rows should be included in the GROUP BY clause, but it refers to the alias of a column value that is not known until after the rows have been selected, and grouped by the GROUP BY.
In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters:
SELECT 1 AS `one`, 2 AS 'two'; Elsewhere in the statement, quoted references to the alias must use identifier quoting or the reference is treated as a string literal. For example, this statement groups by the values in column id, referenced using the alias `a`:
SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name GROUP BY `a`; But this statement groups by the literal string 'a' and will not work as expected:
SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name GROUP BY 'a';
1. It shows COST_REMARK for all the promos in the table. 2. It produces an error because the SUBQUERY gives an error. 3. Access Mostly Uused Products by 50000+ Subscribers 4. It produces an error because SUBQUERIES cannot be used with the case expression.