[READ: Why-JAVA][Start-Learning-JAVA][Why-Java's-Hot][Start-Using-LINUX][Java-Q-&-A][Question][Java-SE-6-Doc][Struts-Doc][Java-Tutorial-Index] [Java-Certification]
[Tech.][Struts.][Servlet.][JSP.][EJB.][JNDI-JMS][SQL.][JDBC.]
CORE-JAVA: [OOP][CLASS][ABSTRACT][EXCEPTIONS][THREADS][UTILS][PACKAGES][JVM][CASTING][NETWORKING][RMI-XML]
[Tech.][Struts.][Servlet.][JSP.][EJB.][JNDI-JMS][SQL.][JDBC.]
CORE-JAVA: [OOP][CLASS][ABSTRACT][EXCEPTIONS][THREADS][UTILS][PACKAGES][JVM][CASTING][NETWORKING][RMI-XML]
What is SQL? (Online Testing) (Faqs-Index) (MySql-Index) (Tech-Index)
A: SQL stands for 'Structured Query Language'.
What is SELECT statement? (Sample-Table)
A: The SELECT statement lets you select a set of values from a table in a database. The values selected from the database table would depend on the various conditions that are specified in the SQL query.
How can you compare a part of the name rather than the entire name?
A: SELECT * FROM people WHERE empname LIKE '%ab%' Would return a recordset with records consisting empname, with the sequence 'ab' in empname.
What is the INSERT statement?
A: The INSERT statement lets you insert information into a database.
How do you delete a record from a database?
A: Use the DELETE statement to remove records or any particular column values from a database.
How could I get distinct entries from a table? (Online Database) (Create Query)
A: The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would of course depend on the various conditions that are specified in the SQL query. Example:
SELECT DISTINCT empname FROM emptable.
How to get the results of a Query sorted in any order? (Index)
A: You can sort the results and return the sorted results to your program by using ORDER BY keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.
SELECT empname, age, city FROM emptable ORDER BY empname.
How can I find the total number of records in a table? (TechOnTheNet) (Index)
A: You could use the COUNT keyword , example:
SELECT COUNT(*) FROM emp WHERE age>40
What is GROUP BY? (MySql) (Faqs)
A: The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible.
What is the difference among "dropping a table", "truncating a table" and "deleting all records" from a table. (Faqs-Index) (MySql-Index)
A: Dropping : (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes. (MySql) (Faqs)
Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete. (MySql) (Faqs)
Delete : (Data alone deleted), Doesn't perform automatic commit. (MySql) (Faqs) (Tech)
What are the Large object types supported by Oracle?
A: Blob and Clob. (Oracle)
Difference between a "where" clause and a "having" clause.
A: HAVING clause is used only with group functions whereas WHERE is not used with. Having Ex:1. Ex2. Where Ex:1. Ex:2.
What's the difference between a primary key and a unique key?
A: 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 as unique creates a non-clustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.
What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?
A: Cursors allow row-by-row processing of the result sets. Types of cursors: Dynamic, Keyset-driven, Static and Forward-only. See below for more information. Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network round-trip, where as a normal SELECT query makes only one round-trip, however large the result-set is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Further, there are restrictions on the SELECT statements that can be used with some types of cursors. Most of the times, set based operations can be used instead of cursors.
The four types of cursors supported by Transact-SQL: DYNAMIC, KEYSET, STATIC, and FORWARD_ONLY. Difference lies with how the data is traversed by the cursor.
(1)Type (2)Scrollable (3)Membership/Order (4)Column Values (5)Description
(1)DYNAMIC/SENSITIVE (2)Yes (3)Dynamic (4)Dynamic
(5)View additions, changes, and deletions by other users while cursor is open.
(1)KEYSET (2)Yes (3)Fixed (4)Dynamic
(5)View changes, but not additions and deletions while the cursor is open.
(1)STATIC / INSENSITIVE (2)Yes (3)Fixed (4)Fixed
(5)View only a copy of the data; cannot view additions, changes, or deletions while cursor is open.
(1)FORWARD_ONLY (default) (2)No (3)Dynamic (4)Dynamic
(5)Same as Static cursor but can only scroll forward through a table.
A DYNAMIC cursor allows us to move freely throughout your record-set, without restrictions, except when our provider does not support bookmarks.
A FORWARD-ONLY cursor (the default) returns rows sequentially from the database. It does not require space in tempdb, and changes made to the underlying data are visible as soon as they're reached.
What are triggers? How to invoke a trigger on demand?
A: Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table. Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined. Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints (Wiki) for this purpose, instead of triggers, as constraints are much faster.
Where do we use these statements?
1. SELECT 2. DISTINCT 3. WHERE 4. AND OR 5. IN 6. BETWEEN 7. LIKE 8. ORDERBY
4, 5, 6 and 7 are used in conjunction with WHERE statement.
8 is used with or without WHERE statement with DESC or ASC.
What’s the meaning of:
*’A_Z’ *’ABC%’ *’%XYZ’ *‘%AN%’
These are used with LIKE keyword.
*’A_Z’ String that starts with A and ends with Z with any single character between them.
*’ABC%’ String that starts with ABC and end with any number of characters.
*’%XYZ’ String that starts with any number of characters and ends with XYZ.
*’%AN%’ String that starts and ends with any number of characters with embedded AN characters.
FUNCTIONS: Used to do math on numbers.
1. AVG(column name) 2. COUNT(column) 3. MAX(column) 4. MIN(column) 5. SUM(column)
These are used with SELECT statements.
Ex: SELECT SUM(Column-Sales) FROM Table-Store_Information. (Sum of all sales values)
SELECT COUNT(Column-Store_Name) FROM Table-Store_Information. (Count number of rows in column)
SELECT COUNT(DISTINCT Column-Store_Name) FROM Table-Store_Information. (Count number of distinct rows in column)
SELECT MAX(Column-Sales) FROM Table-Store_Information. (Get maximum value in column)
SELECT MIN(Column-Sales) FROM Table-Store_Information. (Get minimum value in column)
GROUP BY: Keyword (Used with aggregate functions)
Used to get distinct values in columns when using math functions. Used under Conditions: Selecting multiple columns from table, at least one arithmetic operator is used, GROUP BY is applied at the end of the statement.
HAVING: Keyword (Used with aggregate functions). If it’s not an aggregate function then WHERE can be used.
Ex: SELECT store_name, SUM(sales) FROM Store_Information GROUP BY store_name HAVING SUM(sales) > 1500
Result:
store_name SUM(Sales)
Los Angeles $1800
Alias Names: Used to give easy shortcut names to Columns and Tables. To give easy readable column headings in output results if the column name is big or complicated or when using arithmetic operations like SUM(name) etc. Where as Table alias is used to make clear and easy query statements by replacing the real table names with shortcuts. When querying multiple columns it’s convenient to use alias shortcut names to represent columns from different tables.
SELECT A1.store_name “Store”, SUM(A1.Sales) “Total Sales” FROM Store_Information A1 GROUP BY A1.store_name
* What is a join and explain different types of joins? (Wiki)
A: 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.
Different types of joins are classified based on the display of results.
Types of joins: INNER JOIN (No Null) and OUTER JOIN (Contain Null).
Inner join can be CROSS or EQUI Join.
OUTER JOIN is further classified as LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN.
Join: (or Inner Join): Used when Two Tables have common fields in columns represented by WHERE clause.
GROUP BY columns other than the common columns. (Does not contain NULL in result)
Ex: SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name GROUP BY A1.region_name
Result:
REGION SALES
East $700
West $2050
* Equi-join(Explicit Inner join).
Ex: SELECT * FROM Geography INNER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
(Displays two store_name columns without any NULL values)
* Natural join(implicit inner join).
Ex: SELECT * FROM Geography NATURAL JOIN Store_Information
(Displays single store_name column without any NULL values)
(OR) SELECT * FROM Geography, Store_Information WHERE Geography.store_name = Store_Information.store_name
Outer Join: (Left or Right Join): Used when Two tables have common fields in columns represented by WHERE clause.
GROUP BY columns which are common columns. (May contain NULL in result).
* Left outer join: Contains all records of the "LEFT" table, at least once. NULL will appear if no records are matched from the RIGHT table.
Ex: SELECT * FROM Geography LEFT OUTER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
Ex of Left Outer Join: SELECT A1.store_name, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_name
Result:
store_name SALES
Boston $700
New York Null
Los Angeles $1800
San Diego $250
* Right outer join: Contains every record from the "RIGHT" table, at least once. NULL will appear if no records are matched from the left table.
Ex: SELECT * FROM Geography RIGHT OUTER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
What is a self join?
A: Self join is just like any other join, except that two instances of the same table will be joined in the query.
* SQL Sub query: Used to embed one sql statement within another, with WHERE or HAVING statements to join tables.
Ex: SELECT SUM(Sales) FROM Store_Information
WHERE Store_name IN (SELECT store_name FROM Geography WHERE region_name = ‘west’)
Result:
SUM (Sales)
2050
SQL Union: Used to combine two queries, from different tables to select distinct values. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information UNION SELECT Date FROM Internet_Sales
Result: Distinct values from both tables. (Values without repetitions.)
Note: “SELECT DISTINCT Date” will also give the same result.
SQL Union All: Used to combine two queries, from different tables to select all the values. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales
Result: All the values are displayed. (Not distinct). (Values with repetitions.)
SQL Intersect: Used to select only common distinct values in columns from different tables with two query statements. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information INTERSECT SELECT Date FROM Internet_Sales
Result: Only common distinct values. (Common values present in both tables without repetitions.)
SQL Minus: Used to select only uncommon distinct values in columns from the first table only, with two queries. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information MINUS Date FROM Internet_Sales
Result: Only uncommon distinct values from first table. (Uncommon values from first table without repetitions.)
A: SQL stands for 'Structured Query Language'.
What is SELECT statement? (Sample-Table)
A: The SELECT statement lets you select a set of values from a table in a database. The values selected from the database table would depend on the various conditions that are specified in the SQL query.
How can you compare a part of the name rather than the entire name?
A: SELECT * FROM people WHERE empname LIKE '%ab%' Would return a recordset with records consisting empname, with the sequence 'ab' in empname.
What is the INSERT statement?
A: The INSERT statement lets you insert information into a database.
How do you delete a record from a database?
A: Use the DELETE statement to remove records or any particular column values from a database.
How could I get distinct entries from a table? (Online Database) (Create Query)
A: The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would of course depend on the various conditions that are specified in the SQL query. Example:
SELECT DISTINCT empname FROM emptable.
How to get the results of a Query sorted in any order? (Index)
A: You can sort the results and return the sorted results to your program by using ORDER BY keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.
SELECT empname, age, city FROM emptable ORDER BY empname.
How can I find the total number of records in a table? (TechOnTheNet) (Index)
A: You could use the COUNT keyword , example:
SELECT COUNT(*) FROM emp WHERE age>40
What is GROUP BY? (MySql) (Faqs)
A: The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible.
What is the difference among "dropping a table", "truncating a table" and "deleting all records" from a table. (Faqs-Index) (MySql-Index)
A: Dropping : (Table structure + Data are deleted), Invalidates the dependent objects ,Drops the indexes. (MySql) (Faqs)
Truncating: (Data alone deleted), Performs an automatic commit, Faster than delete. (MySql) (Faqs)
Delete : (Data alone deleted), Doesn't perform automatic commit. (MySql) (Faqs) (Tech)
What are the Large object types supported by Oracle?
A: Blob and Clob. (Oracle)
Difference between a "where" clause and a "having" clause.
A: HAVING clause is used only with group functions whereas WHERE is not used with. Having Ex:1. Ex2. Where Ex:1. Ex:2.
What's the difference between a primary key and a unique key?
A: 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 as unique creates a non-clustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.
What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?
A: Cursors allow row-by-row processing of the result sets. Types of cursors: Dynamic, Keyset-driven, Static and Forward-only. See below for more information. Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network round-trip, where as a normal SELECT query makes only one round-trip, however large the result-set is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Further, there are restrictions on the SELECT statements that can be used with some types of cursors. Most of the times, set based operations can be used instead of cursors.
The four types of cursors supported by Transact-SQL: DYNAMIC, KEYSET, STATIC, and FORWARD_ONLY. Difference lies with how the data is traversed by the cursor.
(1)Type (2)Scrollable (3)Membership/Order (4)Column Values (5)Description
(1)DYNAMIC/SENSITIVE (2)Yes (3)Dynamic (4)Dynamic
(5)View additions, changes, and deletions by other users while cursor is open.
(1)KEYSET (2)Yes (3)Fixed (4)Dynamic
(5)View changes, but not additions and deletions while the cursor is open.
(1)STATIC / INSENSITIVE (2)Yes (3)Fixed (4)Fixed
(5)View only a copy of the data; cannot view additions, changes, or deletions while cursor is open.
(1)FORWARD_ONLY (default) (2)No (3)Dynamic (4)Dynamic
(5)Same as Static cursor but can only scroll forward through a table.
A DYNAMIC cursor allows us to move freely throughout your record-set, without restrictions, except when our provider does not support bookmarks.
A FORWARD-ONLY cursor (the default) returns rows sequentially from the database. It does not require space in tempdb, and changes made to the underlying data are visible as soon as they're reached.
What are triggers? How to invoke a trigger on demand?
A: Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table. Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined. Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints (Wiki) for this purpose, instead of triggers, as constraints are much faster.
Where do we use these statements?
1. SELECT 2. DISTINCT 3. WHERE 4. AND OR 5. IN 6. BETWEEN 7. LIKE 8. ORDERBY
4, 5, 6 and 7 are used in conjunction with WHERE statement.
8 is used with or without WHERE statement with DESC or ASC.
What’s the meaning of:
*’A_Z’ *’ABC%’ *’%XYZ’ *‘%AN%’
These are used with LIKE keyword.
*’A_Z’ String that starts with A and ends with Z with any single character between them.
*’ABC%’ String that starts with ABC and end with any number of characters.
*’%XYZ’ String that starts with any number of characters and ends with XYZ.
*’%AN%’ String that starts and ends with any number of characters with embedded AN characters.
FUNCTIONS: Used to do math on numbers.
1. AVG(column name) 2. COUNT(column) 3. MAX(column) 4. MIN(column) 5. SUM(column)
These are used with SELECT statements.
Ex: SELECT SUM(Column-Sales) FROM Table-Store_Information. (Sum of all sales values)
SELECT COUNT(Column-Store_Name) FROM Table-Store_Information. (Count number of rows in column)
SELECT COUNT(DISTINCT Column-Store_Name) FROM Table-Store_Information. (Count number of distinct rows in column)
SELECT MAX(Column-Sales) FROM Table-Store_Information. (Get maximum value in column)
SELECT MIN(Column-Sales) FROM Table-Store_Information. (Get minimum value in column)
GROUP BY: Keyword (Used with aggregate functions)
Used to get distinct values in columns when using math functions. Used under Conditions: Selecting multiple columns from table, at least one arithmetic operator is used, GROUP BY is applied at the end of the statement.
HAVING: Keyword (Used with aggregate functions). If it’s not an aggregate function then WHERE can be used.
Ex: SELECT store_name, SUM(sales) FROM Store_Information GROUP BY store_name HAVING SUM(sales) > 1500
Result:
store_name SUM(Sales)
Los Angeles $1800
Alias Names: Used to give easy shortcut names to Columns and Tables. To give easy readable column headings in output results if the column name is big or complicated or when using arithmetic operations like SUM(name) etc. Where as Table alias is used to make clear and easy query statements by replacing the real table names with shortcuts. When querying multiple columns it’s convenient to use alias shortcut names to represent columns from different tables.
SELECT A1.store_name “Store”, SUM(A1.Sales) “Total Sales” FROM Store_Information A1 GROUP BY A1.store_name
* What is a join and explain different types of joins? (Wiki)
A: 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.
Different types of joins are classified based on the display of results.
Types of joins: INNER JOIN (No Null) and OUTER JOIN (Contain Null).
Inner join can be CROSS or EQUI Join.
OUTER JOIN is further classified as LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN.
Join: (or Inner Join): Used when Two Tables have common fields in columns represented by WHERE clause.
GROUP BY columns other than the common columns. (Does not contain NULL in result)
Ex: SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name GROUP BY A1.region_name
Result:
REGION SALES
East $700
West $2050
* Equi-join(Explicit Inner join).
Ex: SELECT * FROM Geography INNER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
(Displays two store_name columns without any NULL values)
* Natural join(implicit inner join).
Ex: SELECT * FROM Geography NATURAL JOIN Store_Information
(Displays single store_name column without any NULL values)
(OR) SELECT * FROM Geography, Store_Information WHERE Geography.store_name = Store_Information.store_name
Outer Join: (Left or Right Join): Used when Two tables have common fields in columns represented by WHERE clause.
GROUP BY columns which are common columns. (May contain NULL in result).
* Left outer join: Contains all records of the "LEFT" table, at least once. NULL will appear if no records are matched from the RIGHT table.
Ex: SELECT * FROM Geography LEFT OUTER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
Ex of Left Outer Join: SELECT A1.store_name, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_name
Result:
store_name SALES
Boston $700
New York Null
Los Angeles $1800
San Diego $250
* Right outer join: Contains every record from the "RIGHT" table, at least once. NULL will appear if no records are matched from the left table.
Ex: SELECT * FROM Geography RIGHT OUTER JOIN Store_Information ON Geography.store_name = Store_Information.store_name
What is a self join?
A: Self join is just like any other join, except that two instances of the same table will be joined in the query.
* SQL Sub query: Used to embed one sql statement within another, with WHERE or HAVING statements to join tables.
Ex: SELECT SUM(Sales) FROM Store_Information
WHERE Store_name IN (SELECT store_name FROM Geography WHERE region_name = ‘west’)
Result:
SUM (Sales)
2050
SQL Union: Used to combine two queries, from different tables to select distinct values. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information UNION SELECT Date FROM Internet_Sales
Result: Distinct values from both tables. (Values without repetitions.)
Note: “SELECT DISTINCT Date” will also give the same result.
SQL Union All: Used to combine two queries, from different tables to select all the values. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales
Result: All the values are displayed. (Not distinct). (Values with repetitions.)
SQL Intersect: Used to select only common distinct values in columns from different tables with two query statements. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information INTERSECT SELECT Date FROM Internet_Sales
Result: Only common distinct values. (Common values present in both tables without repetitions.)
SQL Minus: Used to select only uncommon distinct values in columns from the first table only, with two queries. Condition: Selected result data type from both table columns must be same.
Ex: SELECT Date FROM Store_Information MINUS Date FROM Internet_Sales
Result: Only uncommon distinct values from first table. (Uncommon values from first table without repetitions.)
"WE" has More Power than "I". Its a collective effort to create good things in life. Please let me know if you encounter any problems or have any suggestions. Please leave a comment and keep me informed.
[Winning-in-Losing.] [Miracles.] [Life-is-full-of-Tests.] [Never-Lose-Hope.][Interview-With-God.]
Prayers-To-Worship.
Please-do-not-leave-me-ever. Let’s-make-it-happen. Faith-in-Patience. The-only-one-I've-got. Someone-somewhere. How-I-Love-You. Will-You? Successful-Life. Please-say-something. Way-to-Paradise. My-Everything.
No comments:
Post a Comment