4. SQL - 1688813695672
4. SQL - 1688813695672
For
RDBMS -SQL
Website: www.analytixlabs.co.in
Email: [email protected]
Disclaimer: This material is protected under copyright act AnalytixLabs©, 2011-2023. Unauthorized use and/ or duplication
of this material or any part of this material including data, in any form without explicit and written permission from
AnalytixLabs is strictly prohibited. Any violation of this copyright will attract legal actions.
The select statement is used to query the database and retrieve selected data that match the criteria that you specify.
The column names that follow the select keyword determine which columns will be returned in the results. You can select
as many column names that you'd like, or you can use a "*" to select all columns. The table name that follows the
keyword from specifies the table that will be queried to retrieve the desired results.
The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria
described after the keyword where Conditional selections used in the where clause:
= Equal
> Greater than
< Less than
Greater than or
>=
equal
<= Less than or equal
<> Not equal to
LIKE *See note below
The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very
powerful operator that allows you to select only rows that are "like" what you specify. The percent sign "%" can be used
as a wild card to match any possible character that might appear before or after the characters specified. The create table
statement is used to create a new table.
Example:
create table employee
(first varchar(15), last varchar(20), age number(3), address varchar(30),
city varchar(20),
state varchar(20));
The insert statement is used to insert or add a row of data into the table.
Example:
insert into employee
(first, last, age, address, city, state)
values ('Luke', 'Duke', 45, '2130 Boars Nest',
'Hazard Co', 'Georgia');
The update statement is used to update or change records that match specified criteria. This is accomplished by carefully
constructing a where clause.
Examples:
update phone_book
set area_code = 623
where prefix = 979;
Examples:
delete from employee;
where firstname = 'Mike' or firstname = 'Eric';
The drop table command is used to delete a table and all rows in the table.
Example:
drop table myemployees_ts0211;
SELECT Statement
The SELECT statement is used to query the database and retrieve selected data that match the criteria that you specify.
The SELECT statement has five main clauses to choose from, although, FROM is the only required clause. Each of the
clauses has a vast selection of options, parameters, etc. The clauses will be listed below.
Example:
SELECT name, age, salary
FROM employee
WHERE age > 50;
Aggregate Functions
Example:
SELECT AVG(salary)
FROM employee;
WHERE title = 'Programmer';
GROUP BY clause:
The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow
aggregate functions to be performed on the one or more columns.
HAVING clause
The HAVING clause allows you to specify conditions on the rows for each group - in other words, which rows should be
selected will be based on the conditions you specify. The HAVING clause should follow the GROUP BY clause if you are
going to use it.
SELECT dept, avg(salary)
FROM employee
GROUP BY dept
HAVING avg(salary) > 20000;
ORDER BY clause
ORDER BY is an optional clause, which will allow you to display the results of your query in a sorted order (either
ascending order or descending order) based on the columns that you specify to order by.
SELECT employee_id, dept, name, age, salary
FROM employee_info
For example:
SELECT employeeid, firstname, lastname, title, salary
FROM employee_info
WHERE salary >= 50000.00 AND title = 'Programmer';
Another Example:
SELECT firstname, lastname, title, salary
FROM employee_info
WHERE (title = 'Sales') OR (title = 'Programmer');
Q. What is the purpose of the group functions in SQL? Give some examples of group functions.
Ans: Group functions in SQL work on sets of rows and returns one result per group. Examples of group functions are AVG,
COUNT, MAX, MIN, STDDEV, SUM, and VARIANCE.
Q. What is a Database?
Ans: Database is nothing but an organized form of data for easy access, storing, retrieval and managing of data. This is
also known as structured form of data which can be accessed in many ways.
Example: School Management Database, Bank Management Database.
These relations are defined by using “Foreign Keys” in any RDBMS. Many DBMS companies claimed there DBMS product
was a RDBMS compliant, but according to industry rules and regulations if the DBMS fulfills the twelve CODD rules it’s
truly a RDBMS. Almost all DBMS (SQLSERVER, ORACLE etc) fulfills all the twelve CODD rules and are considered as truly
RDBMS.
Q. What is database or database management systems (DBMS)? What’s the difference between file and database? Can
files qualify as a database?
Ans: Database provides a systematic and organized way of storing, managing and retrieving from collection of logically
related information.
Secondly the information has to be persistent, that means even after the application is closed the information should be
persisted.
Finally it should provide an independent way of accessing data and should not be dependent on the application to access
the information.
Main difference between a simple file and database that database has independent way (SQL) of accessing information
while simple files do not File meets the storing, managing and retrieving part of a database but not the independent way
of accessing data. Many experienced programmers think that the main difference is that file cannot provide multi-user
capabilities which a DBMS provides. But if we look at some old COBOL and C programs where file where the only means
of storing data, we can see functionalities like locking, multi-user etc provided very efficiently. So it’s a matter of debate if
some interviewers think this as a main difference between files and database accept it… going in to debate is probably
losing a job.
Q. Can you explain Fourth Normal Form and Fifth Normal Form?
Ans: In fourth normal form it should not contain two or more independent multi-v about an entity and it should satisfy
“Third Normal form”.
Fifth normal form deals with reconstructing information from smaller pieces of information. These smaller pieces of
information can be maintained with less redundancy.
Q. What is Denormalization?
Ans: Denormalization is the process of putting one fact in numerous places (its vice-versa of normalization). Only one
valid reason exists for denormalization a relational design - to enhance performance. The sacrifice to performance is that
you increase redundancy in database.
Data warehouse is a collection of integrated, subject-oriented databases designed to support the decision-support
functions (DSF), where each unit of data is relevant to some moment in time.
Q. What are Fact tables and Dimension Tables? What is Dimensional Modeling and Star Schema Design?
Ans: When we design transactional database we always think in terms of normalizing design to its least form. But when it
comes to designing for Data warehouse we think more in terms of denormalization the database. Data warehousing,
Databases are designed using Dimensional Modeling. Dimensional Modeling uses the existing relational database
structure and builds on that.
Q. What is Snow Flake Schema design in database? What’s the difference between Star and Snow flake schema?
Ans: Star schema is good when you do not have big tables in data warehousing. But when tables start becoming really
huge it is better to denormalize. When you denormalize star schema it is nothing but snow flake design. For instance
below customer address table is been normalized and is a child table of Customer table. Same holds true for Salesperson
table.
Temporary tables are physical tables that are created and stored in the tempdb database. They are used to store
temporary data that persists for the duration of a session or connection.
Temporary tables are explicitly created using the CREATE TABLE statement, and they can have columns, indexes,
constraints, and other attributes like regular tables.
Temporary tables are accessible across multiple queries within the same session or connection. They can be
modified, inserted into, updated, or deleted from, just like regular tables.
Temporary tables need to be explicitly dropped or they will be automatically dropped when the session or
connection is closed or terminated.
Q. What is the difference between a CTE and Temporary tables?
Ans:
- CTEs are virtual tables defined within a single query, while temporary tables are physical tables stored in
the tempdb database.
- CTEs are not accessible outside the query where they are defined, while temporary tables can be accessed
and modified across multiple queries within the same session.
- CTEs are not explicitly created or dropped, while temporary tables need to be explicitly created and
dropped.
- CTEs are typically used for simplifying complex queries and recursive operations, while temporary tables
are used for storing intermediate results or temporary data for more complex data manipulation.
Q. What are the different types of tables used in SQL?
Ans: The following are the table types used in SQL:
Partitioned tables, Temporary tables, System tables, Wide tables
Q. What are the Set Operators?
Ans: There are four types of set operators available in SQL. They are given as follows:
Union : This operator allows combining result sets of two or more SELECT statements.
Union All : This operator allows combining result sets of two or more SELECT statements along with duplicates.
Intersect : This operator returns the common records of the result sets of two or more SELECT statements.
The database server uses a B-tree structure to organize index information. B-Tree generally has following types of index
pages or nodes:
1. root node: A root node contains node pointers to branch nodes which can be only one.
2. branch node: A branch node contains pointers to leaf nodes or other branch nodes which can be two or more.
3. leaf nodes: A leaf node contains index items and horizontal pointers to other leaf nodes which can be many.
Q. I have a table which has lot of inserts, is it a good database design to create indexes on that table? Insert’s are
slower on tables which have indexes, justify it? or Why does page splitting happen?
Ans: All indexing fundamentals in database use “B-tree” fundamental. Now whenever there is new data inserted or
deleted the tree tries to become unbalance.
Creates a new page to balance the tree.
Shuffle and move the data to pages.
So if your table is having heavy inserts that means it’s transactional, then you can visualize the amount of splits it will be
doing. This will not only increase insert time but will also upset the end-user who is sitting on the screen. So when you
forecast that a table has lot of inserts it’s not a good idea to create indexes.
Q. What are the two types of indexes and explain them in detail? or What’s the difference between clustered and non-
clustered indexes?
Ans: There are basically two types of indexes: Clustered Indexes & Non-Clustered Indexes.
In clustered index the non-leaf level actually points to the actual data. In Non-Clustered index the leaf nodes point to
pointers (they are row id’s) which then point to actual data.
Data Definition
View Definition
Data Manipulation (Interactive and by program).
Integrity Constraints
Authorization.
Transaction boundaries ( Begin , commit and rollback)
"Application programs and terminal activities remain logically unimpaired whenever any changes are made in either
storage representations or access methods."
"If a relational system has a low-level (single-record-at-a-time) language, that low level cannot be used to subvert or
bypass the integrity Rules and constraints expressed in the higher level relational language (multiple-records-at-a-time)."
Extraction:-
In this process we extract data from the source. In actual scenarios data source can be in many forms EXCEL, ACCESS,
Delimited text, CSV (Comma Separated Files) etc. So extraction process handle’s the complexity of understanding the data
source and loading it in a structure of data warehouse.
Transformation:-
This process can also be called as cleaning up process. It’s not necessary that after the extraction process data is clean and
valid. For instance all the financial figures have NULL values but you want it to be ZERO for better analysis. So you can
have some kind of stored procedure which runs through all extracted records and sets the value to zero.
Loading:-
After transformation you are ready to load the information in to your final data warehouse database.
Q. Compare Data mining and Data Warehousing?
Ans: “Data Warehousing” is technical process where we are making our data centralized while “Data mining” is more of
business activity which will analyze how good your business is doing or predict how it will do in the future coming times
using the current data. As said before “Data Warehousing” is not a need for “Data mining”. It’s good if you are doing
“Data mining” on a “Data Warehouse” rather than on an actual production database. “Data Warehousing” is essential
when we want to consolidate data from different sources, so it’s like a cleaner and matured data which sits in between
the various data sources and brings then in to one format.
“Data Warehouses” are normally physical entities which are meant to improve accuracy of “Data mining” process. For
example you have 10 companies sending data in different format, so you create one physical database for consolidating
all the data from different company sources, while “Data mining” can be a physical model or logical model. You can create
a database in “Data mining” which gives you reports of net sales for this year for all companies. This need not be a
physical database as such but a simple query.
Q. What are various DDL commands in SQL? Give brief description of their purposes.
Ans: Following are various DDL or Data Definition Language commands in SQL −
CREATE − it creates a new table, a view of a table, or other object in database.
ALTER − it modifies an existing database object, such as a table.
DROP − it deletes an entire table, a view of a table or other object in the database.
Q. What are various DML commands in SQL? Give brief description of their purposes.
Ans: Following are various DML or Data Manipulation Language commands in SQL −
SELECT − it retrieves certain records from one or more tables.
INSERT − it creates a record.
Q. What are various DCL commands in SQL? Give brief description of their purposes.
Ans: Following are various DCL or Data Control Language commands in SQL −
GRANT − it gives a privilege to user.
REVOKE − it takes back privileges granted from user.
Example:
Table: Employee.
Field: Emp ID, Emp Name, Date of Birth.
Data: 201456, David, 11/15/1960.
Q. What is an Index?
Ans: An index is performance tuning method of allowing faster retrieval of records from the table. An index creates an
entry for each value and it will be faster to retrieve data.
Unique Index: This indexing does not allow the field to have duplicate values if the column is unique indexed. Unique
index can be applied automatically when primary key is defined.
Clustered Index: 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. This type of
index reorders the physical order of the table and search based on the key values. Each table can have only one clustered
index.
NonClustered Index: NonClustered Index is a special type of index in which the logical order of the index does not match
the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages.
Instead, the leaf nodes contain index rows. NonClustered Index does not alter the physical order of the table and
maintains logical order of data. Each table can have 999 nonclustered indexes.
Q. What is a Cursor?
Ans: A database Cursor is a control which enables traversal over the rows or records in the table. This can be viewed as a
pointer to one row in a set of rows. Cursor is very much useful for traversing such as retrieval, addition and removal of
database records.
Global variables are the variables which can be used or exist throughout the program. Same variable declared in global
cannot be used in functions. Global variables cannot be created whenever that function is called.
Q. What is a constraint?
Ans: Constraint can be used to specify the limit on the data type of table. Constraint can be specified while creating or
altering the table statement.
Sample of constraint are:
1. NOT NULL
2. CHECK
3. DEFAULT
4. UNIQUE
5. PRIMARY KEY
6. FOREIGN KEY
Q. What is a trigger?
Ans: A DB trigger is a code or programs that automatically execute with response to some event on a table or view in a
database. Mainly, trigger helps to maintain the integrity of the database. Example: When a new student is added to the
student database, new records should be created in the related tables like Exam, Score and Attendance tables.
Q. What is CLAUSE?
Ans: SQL clause is defined to limit the result set by providing condition to the query. This usually filters some rows from
the whole set of records.
Here, st refers to alias name for student table and Ex refers to alias name for exam table.
"_" operator (we can read as “Underscore Operator”). “_” operator is the character defined atthat point. In the below
sample fired a query
Select name from pcdsEmployee where name like
'_s%' So all name where second letter is “s” is returned.
Q. We have an employee salary table how do we find the second highest from it?
Ans: Below Sql Query find the second highest salary
SELECT * FROM pcdsEmployeeSalary a WHERE (2=(SELECT COUNT(DISTINCT(b.salary)) FROM
pcdsEmployeeSalary b WHERE b.salary>=a.salary))
There are various types of join which can be used to retrieve data and it depends on the relationship between tables.
Inner join: Inner join return rows when there is at least one match of rows between the tables.
Right Join: Right join return rows which are common between the tables and all rows of Right hand side table. Simply, it
returns all the rows from the right hand side table even though there are no matches in the left hand side table.
Left Join: Left join return rows which are common between the tables and all rows of Left hand side table. Simply, it
returns all the rows from Left hand side table even though there are no matches in the Right hand side table.
Full Join: Full join return rows when there are matching rows in any one of the tables. This means, it returns all the rows
from the left hand side table and all the rows from the right hand side table.
Q. What is “CROSS JOIN”? or What is Cartesian product?
Ans: “CROSS JOIN” or “CARTESIAN PRODUCT” combines all rows from both tables. Number of rows will be product of the
number of rows in each table. In real life scenario I cannot imagine where we will want to use a Cartesian product. But
there are scenarios where we would like permutation and combination probably Cartesian would be the easiest way to
achieve it.
Q. What is Self-Join?
A.Self-join is set to be query used to compare to itself. This is used to compare values in a column with other values in the
same column in the same table. ALIAS ES can be used for the same table comparison.
Q. What is a query?
Ans: A DB query is a code written in order to get the information back from the database. Query can be designed in such a
way that it matched with our expectation of the result set. Simply, a question to the Database.
There are two types of subquery – Correlated and Non-Correlated. A correlated subquery cannot be considered as
independent query, but it can refer the column in a table listed in the FROM the list of the main query. A Non-Correlated
sub query can be considered as independent query and the output of subquery are substituted in the main query.
For example, to retrieve all EmployeeID and CustomerID records from the ORDERS table that have the EmployeeID
greater than the average of the EmployeeID field, you can create a nested query, as shown:
SELECT DISTINCT EmployeeID, CustomerID FROM ORDERS WHERE EmployeeID> (SELECTAVG(EmployeeID) FROM ORDERS)
Q. What are the properties and different Types of Sub-Queries?
Ans: Properties of Sub-Query:
1. A sub-query must be enclosed in the parenthesis.
2. A sub-query must be put in the right hand of the comparison operator, and
3. A sub-query cannot contain an ORDER-BY clause.
Q. Is a NULL value same as zero or a blank space? If not then what is the difference?
Ans: A NULL value is not same as zero or a blank space. A NULL value is a value which is ‘unavailable, unassigned,
unknown or not applicable’. Whereas, zero is a number and blank space is a character.
Q. Say True or False. Give explanation if False. If a column value taking part in an arithmetic expression is NULL, then
the result obtained would be NULLM.
Ans: True.
Q. If a table contains duplicate rows, does a query result display the duplicate values by default? How can you
eliminate duplicate rows from a query result?
Ans: A query result displays all rows including the duplicate rows. To eliminate duplicate rows in the result, the DISTINCT
keyword is used in the SELECT clause.
Q. How do you search for a value in a database table when you don’t have the exact value to search for?
Ans: In such cases, the LIKE condition operator is used to select rows that match a character pattern. This is also called
‘wildcard’ search.
Q. What is the default ordering of data using the ORDER BY clause? How could it be changed?
Ans: The default sorting order is ascending. It can be changed using the DESC keyword, after the column name in the
ORDER BY clause.
Q. What is the difference between the NVL and the NVL2 functions?
Ans: The NVL(exp1, exp2) function converts the source expression (or value) exp1 to the target expression (or value)
exp2, if exp1 contains NULL. The return value has the same data type as that of exp1.
Q. Which expressions or functions allow you to implement conditional processing in a SQL statement?
Ans: There are two ways to implement conditional processing or IF-THEN-ELSE logic in a SQL statement.
Using CASE expression
Q. You want to display a result query from joining two tables with 20 and 10 rows respectively. Erroneously you forget
to write the WHERE clause. What would be the result?
Ans: The result would be the Cartesian product of two tables with 20 x 10 = 200 rows.
Q. Which SQL statement is used to add, modify or drop columns in a database table?
Ans: The ALTER TABLE statement.
Ans: It doesn’t have a GROUP BY clause. The subject_code should be in the GROUP BY clause.
SELECT subject_code, count(name)
FROM students
GROUP BY subject_code;
Ans: The WHERE clause cannot be used to restrict groups. The HAVING clause should be used.
SELECT subject_code, AVG (marks)
FROM students
HAVING AVG(marks) > 75
GROUP BY subject_code;
Q. What happens if you omit the WHERE clause in the UPDATE statement?
Ans: All the rows in the table are modified.
Q. Can you modify the rows in a table based on values from another table? Explain.
Ans: Yes. Use of subqueries in UPDATE statements allow you to update rows in a table based on values from another
table.
Q. Can you remove rows from a table based on values from another table? Explain.
Ans: Yes, subqueries can be used to remove rows from a table based on values from another table.
Q. The ADD command is used to enter one row of data or to add multiple rows as a result of a query.
A. True
B. False
Q. SQL provides the AS keyword, which can be used to assign meaningful column names to the results of queries using
the SQL built-in functions.
A. True
B. False
Answer: Option A
Q. The SELECT command, with its various clauses, allows users to query the data contained in the tables and ask many
different questions or ad hoc queries.
A. True
B. False
Answer: Option A
Q. A SELECT statement within another SELECT statement and enclosed in square brackets ([...]) is called a subquery.
A. True
B. False
Answer: Option B
Q. The rows of the result relation produced by a SELECT statement can be sorted, but only by one column.
A. True
B. False
Answer: Option B
Q. There is an equivalent join expression that can be substituted for all subquery expressions.
A. True
B. False
Answer: Option B
Q. Each index consumes extra storage space and also requires overhead maintenance time whenever indexed data
change value.
A. True
Q. The HAVING clause acts like a WHERE clause, but it identifies groups that meet a criterion, rather than rows.
A. True
B. False
Answer: Option A
Q. The qualifier DISTINCT must be used in an SQL statement when we want to eliminate duplicate rows.
A. True
B. False
Answer: Option A
Q. DISTINCT and its counterpart, ALL, can be used more than once in a SELECT statement.
A. True
B. False
Answer: Option B
Q. COUNT(field_name) tallies only those rows that contain a value; it ignores all null values.
A. True
B. False
Answer: Option A
Q. SUM, AVG, MIN, and MAX can only be used with numeric columns.
A. True
B. False
Answer: Option B
Q. Most companies keep at least two versions of any database they are using.
A. True
B. False
Answer: Option A
Q. The SQL statement: SELECT Number1 + Number 2 AS Total FROM NUMBER_TABLE; adds two numbers from each
row together and lists the results in a column named Total.
A. True
B. False
Answer: Option A
Q. Data manipulation language (DML) commands are used to define a database, including creating, altering, and
dropping tables and establishing constraints.
A. True
B. False
Answer: Option B
Q. Scalar aggregate are multiple values returned from an SQL query that includes an aggregate function.
A. True
B. False
Answer: Option B
Q. The keyword LIKE can be used in a WHERE clause to refer to a range of values.
A. True
B. False
Answer: Option B
Q. The SQL statement: SELECT Name, COUNT(*) FROM NAME_TABLE; counts the number of name rows and displays
this total in a table with a single row and a single column.
A. True
B. False
Answer: Option B
Q. The SQL keyword GROUP BY instructs the DBMS to group together those rows that have the same value in a column.
A. True
B. False
Answer: Option A
Q. The wildcard asterisk (*) is the SQL-92 standard for indicating "any sequence of characters."
A. True
B. False
Answer: Option B
Q. Microsoft Access has become ubiquitous, and being able to program in Access is a critical skill.
A. True
B. False
Answer: Option B
Q. SQL provides five built-in functions: COUNT, SUM, AVG, MAX, MIN.
Q. The keyword BETWEEN can be used in a WHERE clause to refer to a range of values.
A. True
B. False
Answer: Option A
Q. If you are going to use a combination of three or more AND and OR conditions, it is often easier to use the NOT and
NOT IN operators.
A. True
B. False
Answer: Option A
Q. You can add a row using SQL in a database with which of the following?
A. ADD
B. CREATE
C. INSERT
D. MAKE
Answer: Option C
Q. Which of the following is the correct order of keywords for SQL SELECT statements?
Q. Which of the following are the five built-in functions provided by SQL?
A.COUNT, SUM, AVG, MAX, MIN
B.SUM, AVG, MIN, MAX, MULT
C.SUM, AVG, MULT, DIV, MIN
D.SUM, AVG, MIN, MAX, NAME
Answer: Option A
Q. In an SQL SELECT statement querying a single table, according to the SQL-92 standard the asterisk (*) means that:
A. all columns of the table are to be returned.
B. all records meeting the full criteria are to be returned.
C. all records with even partial criteria met are to be returned.
D.None of the above is correct.
Answer: Option A
Q. To remove duplicate rows from the results of an SQL SELECT statement, the ________ qualifier specified must be
included.
A. ONLY
Q. Which of the following do you need to consider when you make a table in SQL?
A.Data types
B.Primary keys
C.Default values
D.All of the above.
Answer: Option D
Q. When three or more AND and OR conditions are combined, it is easier to use the SQL keyword(s):
A. LIKE only.
B. IN only.
C. NOT IN only.
D. Both IN and NOT IN.
Answer: Option D
Q. Find the SQL statement below that is equal to the following: SELECT NAME FROM CUSTOMER WHERE STATE = 'VA';
A.SELECT NAME IN CUSTOMER WHERE STATE IN ('VA');
B.SELECT NAME IN CUSTOMER WHERE STATE = 'VA';
C.SELECT NAME IN CUSTOMER WHERE STATE = 'V';
D.SELECT NAME FROM CUSTOMER WHERE STATE IN ('VA');
Answer: Option D
Q. The SQL statement that queries or reads data from a table is ________ .
A. SELECT
B.READ
C. QUERY
D.None of the above is correct.
Answer: Option A
Q. SQL is:
A.a programming language.
B.an operating system.
C.a data sublanguage.
D. a DBMS.
Answer: Option C