Sei sulla pagina 1di 5

SQL - FAQ

FAQs
1. What is the difference between oracle,sql and sql server ? SQLServer is an RDBMS just like oracle,DB2 from Microsoft Structured Query Language (SQL), pronounced "sequel", is a language that provides an interface to relational database systems. It was developed by IBM in the 1970s for use in System R. SQL is a de facto standard, as well as an ISO and ANSI standard. SQL is used to perform various operations on RDBMS. 2. tables? One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships. One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships. Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table. It will be a good idea to read up a database designing fundamentalstext book. 3. What's the difference between a primary key and a unique key? How do you implement one-to-one, one-to-many and many-to-many relationships while designing

Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only. 4. Define candidate key, alternate key, composite key

A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys. A key formed by combining at least two or more columns is calledcomposite key. 5. What are defaults? Is there a column to which a default can't be bound?

A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can't have defaults bound to them. See CREATE DEFUALT in books online. 6. What is the difference between a HAVING CLAUSE and a WHERE CLAUSE? Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query. WHERE Clause is applied to each row before they are part of the GROUP BY function in a query. 7. What is a transaction and what are ACID properties?

A transaction is a logical unit of work in which, all the steps must be performed or none. ACID stands for Atomicity, Consistency, Isolation, Durability. These are the properties of a transaction. For more information and explanation of these properties. 8. CREATE INDEX myIndex ON myTable(myColumn). What type of Index will get created after

executing the above statement? Non-clustered index. Important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise. 9. How can you Change SQL prompt name

SQL> set sqlprompt Manimara >

www.dwhtechstudy.com

info@dwhtechstudy.com

SQL - FAQ

10.

What's the difference between DELETE, DROP and TRUNCATE commands?

DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back. 11. What are constraints? Explain different types of constraints?

Constraints enable the RDBMS enforce the integrity of the database automatically, without needing you to create triggers, rule or defaults. Types of constraints: NOT NULL, CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY 12. What is an index? What are the types of indexes?

indexes helps to retrieve the data quicker.Indexes are of two types. Clustered indexes and non-clustered indexes.When you craete a clustered index on a table, all the rows in the table are stored in the order of the clustered index key. So, there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Non-clustered indexes are stored as B-tree structures (so do clustered indexes), with the leaf level nodes having the index key and it's row locater. The row located could be the RID or the Clustered index key, depending up on the absence or presence of clustered index on the table. 13. Write down the general syntax for a SELECT statements covering all the options

Here's the basic syntax: (Also checkout SELECT in books online for advanced syntax). SELECT select_list [INTO new_table_] FROM table_source [WHERE search_condition] [GROUP BY group_by__expression] [HAVING search_condition] [ORDER BY order__expression [ASC | DESC] ] 14. What is a join and explain different types of joins

Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS. 15. What is a Transaction? Can you have a nested transaction?

A transaction is a logical unit of work that comprises one or more SQL statements executed by a single user. Yes we can have nested Transactions. 16. What is a self join? Explain it with an example Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join. CREATE TABLE emp ( empid int, mgrid int,

www.dwhtechstudy.com

info@dwhtechstudy.com

SQL - FAQ

empname char(10) ) INSERT emp SELECT 1,2,'Vyas' INSERT emp SELECT 2,3,'Mohan' INSERT emp SELECT 3,NULL,'Shobha' INSERT emp SELECT 4,2,'Shridhar' INSERT emp SELECT 5,2,'Sourabh' SELECT t1.empname [Employee], t2.empname [Manager] FROM emp t1, emp t2 WHERE t1.mgrid = t2.empid 17. What is Cross Join?

A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price. 18. How to display duplicate rows in a table ?

select * from emp where EmpID in(select EmpID from emp group by EmpID having count(EmpID)>1) 19. How do you return the top-N results of a query in Oracle? SELECT * FROM (SELECT * FROM my_table ORDER BY col_name_1 DESC) WHERE ROWNUM < 10; 20. How can you get N
th

Highest value from a table?

Ex: Get Nth Highest salary from emp table select sal from (select distinct(sal) from emp where sal is NOT NULL order by sal dsc) where rownum=n 21. Difference between a "where" clause and a "having" clause.

HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY clause. The WHERE clause is used when you want to specify a condition for columns, single row functions except group functions and it is written before GROUP BY clause if it is used. 22. What is difference between Rename and Alias?

Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or column which do not exist once the SQL statement is executed. 23. What are the advantages and disadvantages of primary key and foreign key in SQL?

Primary key Advantages - It is a unique key on which all the other candidate keys are functionally dependent Disadvantage - There can be more than one keys on which all the other attributes are dependent on. Foreign Key Advantage - It allows refrencing another table using the primary key for the other table 24. What's the difference between a primary key and a unique key?

Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only. 25. 26. How to get the results of a Query sorted in any order? How can you compare a part of the name rather than the entire name?

Using Order by clause we can sort results of a query.

www.dwhtechstudy.com

info@dwhtechstudy.com

SQL - FAQ

The % and _ symbols can be used in any combination with literal characters. 27. 28. why you need indexing and where that is stroed? what you mean by schema object? . We can create indexes explicitly to speed up sql statement execution on a table A schema is a collection of logical structures of data, or schema objects. A schema is owned by a database user and has the same name as that user. Each user owns a single schema. Schema objects can be created and manipulated 29. 30. What the difference between UNION and UNIONALL? Explain normalization ? Union will remove the duplicate rows from the result set while Union all does'nt. Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design, the rocess of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships. 31. What is diffrence between Co-related sub query and nested sub query?

- Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query. - Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row. 32. 33. 34. How can i hide a particular table name of our schema? Which date function is used to find the difference between two dates? Which date function returns number value? you can hide the table name by creating synonyms. DateDiff is the command used to get difference between dates

months_between 35. How does one implement IF-Then-Else in select Statement Ex 1: Ex 2: 36. select decode(sex, 'M', 'Male', 'F', 'Female', 'Unknown') from employees; select a, b, decode( abs(a-b), a-b, 'a > b', 0, 'a = b', 'a < b') from tableX;

How to use "CASE" keyword in SELECT statement? SELECT ename, CASE WHEN sal>1000 THEN 'Over paid' ELSE 'Under paid' END FROM emp;

37. No 38.

can you create a foreign key between columns of two tables which are having different data types ? What is a sub query?

Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword. A subquery SELECT statement can return any number of values, and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. Properties of Sub-Query A subquery must be enclosed in the parenthesis. A subquery must be put in the right hand of the comparison operator, and

www.dwhtechstudy.com

info@dwhtechstudy.com

SQL - FAQ

A subquery cannot contain a ORDER-BY clause. A query can contain more than one sub-queries. 39. How can you make a column as unused column in a table?

To make a column as unused - ALTER TABLE table_name DROP UNUSED COLUMNS; To Drop Unused column - ALTER TABLE table_name SET UNUSED (column_name); 40. What is the purpose of Round()? Can you use it for dates? If so How can ?

Rounds a numeric field to the number of decimals specified. Yes. We can also use Round function with dates.For more information refer 3.4 41. What is the difference between clustered and a non-clustered index? A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages. A nonclustered index is a special type of index in which the logical order of the index does not matchthe physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows. 42. How to implement one-to-one, one-to-many and many-to-many relationships while esigning tables?

One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships. One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships. Many-to-Many relationships are implemented using a junction table with the keys from both the tables
43. What is De-normalization?

De-normalization is the process of attempting to optimize the performance of a database by adding redundant data. It is sometimes necessary because current DBMSs implement the relational model poorly. A true relational DBMS would allow for a fully normalized database at the logical level, while providing physical storage of data that is tuned for high performance. De-normalization is a technique to move from higher to lower normal forms of database modeling in order to speed up database access.

www.dwhtechstudy.com

info@dwhtechstudy.com

Potrebbero piacerti anche