You are on page 1of 11

SQL Interview Questions & Answers

Also read => All about Database Testing


Q#1. What does SQL stand for?
Ans. SQL stands for Structured Query Language.
Q#2. How to select all records from the table?
Ans. To select all the records from the table we need to use following syntax:
Select * from table_name;
Q#3. Define join and name different type of joins?
Ans. Join keyword is used to fetch data from related two or more tables. It returns rows where there is at least one
match in both the tables included in join. Read more here.
Type of joins areRight Join
Outer Join
Full Join
Cross Join
Self Join.
Q#4. What is the syntax to add record to a table?
Ans. To add record in a table INSERT syntax is used.
Ex: INSERT into table_name VALUES (value1, value2..);
Q#5. How do you add a column to a table?
Ans. To add another column in the table following command has been used.
ALTER TABLE table_name ADD (column_name);
Q#6. Define SQL Delete statement.
Ans. Delete is used to delete a row or rows from a table based on the specified condition.
Basic syntax is as follows:
DELETE FROM table_name
WHERE <Condition>
Q#7. Define COMMIT ?
Ans. COMMIT saves all changes made by DML statements.
Q#8. What is a primary key?
Ans. A Primary key is column whose values uniquely identify every row in a table. Primary key values can never be
reused.
Q#9. What are foreign keys?
Ans. When a one tables primary key field is added to related tables in order to create the common field which relates
the two tables, it called a foreign key in other tables.
Foreign Key constraints enforce referential integrity.
Q#10. What is CHECK Constraint?

Ans. A CHECK constraint is used to limit the values or type of data that can be stored in a column. They are used to
enforce domain integrity.
Q#11. Is it possible for a table to have more than one foreign key?
Ans. Yes, a table can have many foreign keys and only one primary key.
Q#12. What are the possible values for BOOLEAN data field.
Ans. For a BOOLEAN data field two values are possible: -1(true) and 0(false).
Q#13. What is a stored procedure?
Ans. A stored procedure is a set of SQL queries which can take input and send back output.
Q#14. What is identity in SQL?
Ans. An identity column in the SQL automatically generates numeric values. We can defined a start and increment
value of identity column.
Q#15. What is Normalization?
Ans. The process of table design to minimize the data redundancy is called normalization. We need to divide a
database into two or more table and define relationships between them.
Q#16. What is Trigger?
Ans. Trigger allows us to execute a batch of SQL code when a table event occurs (Insert, update or delete command
executed against a specific table)
Q#17. How to select random rows from a table?
Ans. Using SAMPLE clause we can select random rows.
Example:
SELECT * FROM table_name SAMPLE(10);
Q#18. Which TCP/IP port does SQL Server run?
Ans. By default SQL Server runs on port 1433.
Q#19. Write a SQL SELECT query that only returns each name only once from a table?
Ans. To get the each name only once, we need to use the DISTINCT keyword.
SELECT DISTINCT name FROM table_name;
Q#20. Explain DML and DDL?
Ans. DML stands for Data Manipulation Language. INSERT, UPDATE and DELETE are DML statements.
DDL stands for Data Definition Language. CREATE ,ALTER, DROP, RENAME are DDL statements.
Q#21. Can we rename a column in the output of SQL query?
Ans. Yes using the following syntax we can do this.
SELECT column_name AS new_name FROM table_name;
Q#22. Give the order of SQL SELECT ?
Ans. Order of SQL SELECT clauses is: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Only the
SELECT and FROM clause are mandatory.
Q#23. Suppose a Student column has two columns, Name and Marks. How to get name and marks of top
three students.

Ans. SELECT Name, Marks FROM Student s1 where 3 <= (SELECT COUNT(*) FROM Students s2 WHERE
s1.marks = s2.marks)
Q#24. What is SQL comments?
Ans. SQL comments can be put by two consecutive hyphens ().
Q#25. Difference between TRUNCATE, DELETE and DROP commands?
Ans. DELETE removes some or all rows from a table based on the condition. It can be rolled back.
TRUNCATE removes ALL rows from a table by de-allocating the memory pages. The operation cannot be rolled back
DROP command removes a table from the database completely.
Q#26. What are the properties of a transaction?
Ans. Generally these properties are referred as ACID properties. They are:
Atomicity
Consistency
Isolation
Durability.
Q#27. What do you mean by ROWID ?
Ans. Its a 18 character long pseudo column attached with each row of a table.
Q#28. Define UNION, MINUS, UNION ALL, INTERSECT ?
Ans. MINUS returns all distinct rows selected by the first query but not by the second.
UNION returns all distinct rows selected by either query
UNION ALL returns all rows selected by either query, including all duplicates.
INTERSECT returns all distinct rows selected by both queries.
-----------Q#29. What is a transaction?
Ans. A transaction is a sequence of code that runs against a database. It takes database from one consistent state to
another.
Q#30. What is difference between UNIQUE and PRIMARY KEY constraints?
Ans. A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys.
Primary key cannot contain Null values whereas Unique key can contain Null values.
Q#31. What is a composite primary key?
Ans. Primary key created on more than one column is called composite primary key.
Q#32. What is an Index ?
Ans. An Index is an special structure associated with a table speed up the performance of queries. Index can be
created on one or more columns of a table.
Q#33. What is the Subquery ?
Ans. A Subquery is sub set of select statements whose return values are used in filtering conditions of the main
query.

Q#34. What do you mean by query optimization?


Ans. Query optimization is a process in which database system compares different query strategies and select the
query with the least cost.
Q#35. What is Collation?
Ans. Set of rules that defines how data is stored, how case sensitivity and Kana character can be treated etc.
Q#36. What is Referential Integrity?
Ans. Set of rules that restrict the values of one or more columns of the tables based on the values of primary key or
unique key of the referenced table.
Q#37. What is Case Function?
Ans. Case facilitates if-then-else type of logic in SQL. It evaluates a list of conditions and returns one of multiple
possible result expressions.
Q#38. Define a temp table?
Ans. A temp table is a temporary storage structure to store the data temporarily.
Q#39. How we can avoid duplicating records in a query?
Ans. By using DISTINCT keyword duplicating records in a query can be avoided.
Q#40. Explain the difference between Rename and Alias?
Ans. Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or
column.
Q#41. What is a View?
Ans. A view is a virtual table which contains data from one or more tables. Views restrict data access of table by
selecting only required values and make complex queries easy.
Q#42. What are the advantages of Views?
Ans. Advantages of Views:
Views restrict access to the data because the view can display selective columns from the table.
Views can be used to make simple queries to retrieve the results of complicated queries. For example, views can
be used to query information from multiple tables without the user knowing.
Q#43. List the various privileges that a user can grant to another user?
Ans. SELECT, CONNECT, RESOURCES.
Q#44. What is schema?
Ans. A schema is collection of database objects of a User.
Q#45. What is Table ?
Ans. A table is the basic unit of data storage in the database management system. Table data is stored in rows and
columns.
Q#46. Do View contain Data?
Ans. No, Views are virtual structure.
Q#47. Can a View based on another View?
Ans. Yes, A View is based on another View.
Q#48. What is difference between Having clause and Where clause?
Ans. Both specify a search condition but Having clause is used only with the SELECT statement and typically used
with GROUP BY clause.

If GROUP BY clause is not used then Having behaves like WHERE clause only.
Q#49. What is difference between Local and Global temporary table?
Ans. If defined in inside a compound statement a local temporary table exists only for the duration of that statement
but a global temporary table exists permanently in the db but its rows disappears when the connection is closed.
Q#50. What is CTE?
Ans. A CTE or common table expression is an expression which contains temporary result set which is defined in a
SQL statement
What is a Data warehouse?
A: A Data warehouse is a repository of integrated information, available for queries and analysis.
Data and information are extracted from heterogeneous sources and stored in a database for easy and more efficient
way to run queries and create reports.
A data warehouse is a logical collection of information gathered from many different operational databases used to
create business intelligence that supports business analysis activities and decision-making tasks, primarily, a record
of an enterprises past transactional and operational information, stored in a database designed to favour efficient
data analysis and reporting (especially OLAP).
Q: What is data mining?
A: Data mining is a process of extracting hidden trends within a datawarehouse. For example an insurance data
warehouse can be used to mine data for the most high risk people to insure in a certain geographial area.
Q: What are Data Marts?
A: Data Marts are subset of the corporate-wide data that is of value to a specific group of users.
There are two types of Data Marts:
1.Independent data marts sources from data captured form OLTP system, external providers or from data
generated locally within a particular department or geographic area.
2.Dependent data mart sources directly from enterprise data warehouses.
Q: What is OLTP?
A: OnLine Transactional Processing.
Q: What is OLAP?
A: OnLine Analatical Processing.
Q: What are the differences between OLTP and OLAP?
A: Main Differences between OLTP and OLAP are:1. User and System Orientation
OLTP: customer-oriented, used for data analysis and querying by clerks, clients and IT professionals.
OLAP: market-oriented, used for data analysis by knowledge workers( managers, executives, analysis).
2. Data Contents
OLTP: manages current data, very detail-oriented.
OLAP: manages large amounts of historical data, provides facilities for summarization and aggregation, stores
information at different levels of granularity to support decision making process.
3. Database Design
OLTP: adopts an entity relationship(ER) model and an application-oriented database design.
OLAP: adopts star, snowflake or fact constellation model and a subject-oriented database design.
4. View
OLTP: focuses on the current data within an enterprise or department.
OLAP: spans multiple versions of a database schema due to the evolutionary process of an organization; integrates
information from many organizational locations and data stores
Q: What is real time data-warehousing?
A: Real-time data warehousing captures business activity data as it occurs. As soon as the business activity is
complete and there is data about it, the completed activity data flows into the data warehouse and becomes available
instantly. In other words, real-time data warehousing is a framework for deriving information from data as the data
becomes available.
Q: What are the steps to build the datawarehouse ?

A:
Gathering business requirements.
Identifying Sources
Identifying Facts
Defining Dimensions
Define Attributes
Redefine Dimensions & Attributes
Organize Attribute Hierarchy & Define Relationship
Assign Unique Identifiers
Additional conventions:Cardinality/Adding ratios
Q: What is a CUBE in data warehousing concept?
A: Cubes are logical representation of multidimensional data.The edge of the cube contains dimension members and
the body of the cube contains data values.
Q: What is a linked cube?
A: Linked cube is a cube, in which a sub-set of the data can be analysed into greater detail. The linking ensures that
the data in the cubes remain consistent.
Q: What is the main difference between Inmon and Kimball philosophies of data warehousing?
A: Both differed in the concept of building the datawarehosue.
Kimball views data warehousing as a constituency of Data marts. Data marts are focused on delivering business
objectives for departments in the organization. And the data warehouse is a conformed dimension of the data marts.
Hence a unified view of the enterprise can be obtain from the dimension modeling on a local departmental level.
Inmon beliefs in creating a data warehouse on a subject-by-subject area basis. Hence the development of the data
warehouse can start with data from the online store. Other subject areas can be added to the data warehouse as
their needs arise.
i.e.,
KimballFirst DataMartsCombined way Datawarehouse
InmonFirst DatawarehouseLater-Datamarts
Q: What is Hierarchy in data warehouse terms?
A: Hierarchies are logical structures that use ordered levels as a means of organizing data. A hierarchy can be used
to define data aggregation. For example, in a time dimension, a hierarchy might aggregate data from the month level
to the quarter level to the year level. A hierarchy can also be used to define a navigational drill path and to establish a
family structure.
Within a hierarchy, each level is logically connected to the levels above and below it. Data values at lower levels
aggregate into the data values at higher levels. A dimension can be composed of more than one hierarchy. For
example, in the product dimension, there might be two hierarchiesone for product categories and one for product
suppliers.
Dimension hierarchies also group levels from general to granular. Query tools use hierarchies to enable you to drill
down into your data to view different levels of granularity. This is one of the key benefits of a data warehouse.
When designing hierarchies, you must consider the relationships in business structures. For example, a divisional
multilevel sales organization.
Hierarchies impose a family structure on dimension values. For a particular level value, a value at the next higher
level is its parent, and values at the next lower level are its children. These familial relationships enable analysts to
access data quickly.
Q: What are the differnces between a RDBMS schema and a data warehouse schema?
A:
RDBMS Schema
* Used for OLTP systems
* Highly Normalized
* Difficult to understand and navigate
* Difficult to extract and solve complex problems
DWH Schema
* Used for OLAP systems
* De-normalized

* Easy to understand and navigate


* Relatively easier in extracting the data and solving complex problems
Q:What is meant by metadata in the context of a Data warehouse?
A: Meta data is the data about data; Business Analyst or data modeler usually capture information about data the
source (where and how the data is originated), nature of data (char, varchar, nullable, existance, valid values etc) and
behavior of data (how it is modified / derived and the life cycle ) in data dictionary a.k.a metadata. Metadata is also
presented at the Datamart level, subsets, fact and dimensions, ODS etc. For a DW user, metadata provides vital
information for analysis / DSS.

What are the commonly used indexes in Data warehouse systems?


A. B-Tree and Bit Map indexes.
Q. When Bit Map indexes are used?
A. The main advantage of using bitmap index is for the columns in which the ratio of the number of
distinct values to the number of rows in the table is very low. This ratio referred as degree of
cardinality. Example: A gender column, which has only two distinct values (male and female), which
is ideal for a bitmap index.
Q. Which scenarios are better suited for using B-Tree indexes?
A. B-tree indexes can be used when a typical query refers to the indexed column and retrieves a few
rows. In these queries, it is faster to find the rows by looking at the index.
What is a dimension table?
A dimensional table is a collection of hierarchies and categories along which the user can drill down
and drill up. it contains only the textual attributes.
What is Fact table?
Fact Table contains the measurements or metrics or facts of business process.
If your business process is Sales , then a measurement of this business process such as monthly
sales number is captured in the Fact table.
Fact table also contains the foriegn keys for the dimension tables.
What is conformed fact?
Conformed dimensions are the dimensions which can be used across multiple Data Marts in
combination with multiple facts tables accordingly
What is ODS?
1. ODS means Operational Data Store.
2. A collection of operation or bases data that is extracted from operation databases and
standardized, cleansed, consolidated, transformed, and loaded into an enterprise data architecture.
An ODS is used to support data mining of operational data, or as the store for base data that is
summarized for a data warehouse. The ODS may also be used to audit the data warehouse to
assure summarized and derived data is calculated properly. The ODS may further become the
enterprise shared operational database, allowing operational systems that are being reengineered to
use the ODS as there operation databases.
What is a lookup table?

A lookUp table is the one which is used when updating a warehouse. When the lookup is placed on
the target table (fact table / warehouse) based upon the primary key of the target, it just updates the
table by allowing only new records or updated records based on the lookup condition.
What are Aggregate tables?
Aggregate table contains the summary of existing warehouse data which is grouped to certain levels
of dimensions.Retrieving the required data from the actual table, which have millions of records will
take more time and also affects the server performance.To avoid this we can aggregate the table to
certain required level and can use it.This tables reduces the load in the database server and
increases the performance of the query and can retrieve the result very fastly.
What are conformed dimensions?
Answer1:
Conformed dimensions mean the exact same thing with every possible fact table to which they are
joined Ex:Date Dimensions is connected all facts like Sales facts,Inventory facts..etc
Answer2:
Conformed dimentions are dimensions which are common to the cubes.(cubes are the schemas
contains facts and dimension tables)
Consider Cube-1 contains F1,D1,D2,D3 and Cube-2 contains F2,D1,D2,D4 are the Facts and
Dimensions here D1,D2 are the Conformed Dimensions
What is a level of Granularity of a fact table?
Level of granularity means level of detail that you put into the fact table in a data warehouse. For
example: Based on design you can decide to put the sales data in each transaction. Now, level of
granularity would mean what detail are you willing to put for each transactional fact. Product sales
with respect to each minute or you want to aggregate it upto minute and put that data.
How are the Dimension tables designed?
Most dimension tables are designed using Normalization principles upto 2NF. In some instances
they are further normalized to 3NF.
Find where data for this dimension are located.
Figure out how to extract this data.
Determine how to maintain changes to this dimension (see more on this in the next section).
What are non-additive facts?
Non-Additive: Non-additive facts are facts that cannot be summed up for any of the dimensions
present in the fact table.
What are slowly changing dimensions?
SCD stands for Slowly changing dimensions. Slowly changing dimensions are of three types
SCD1: only maintained updated values.
Ex: a customer address modified we update existing record with new address.
SCD2: maintaining historical information and current information by using
A) Effective Date
B) Versions

C) Flags
or combination of these
scd3: by adding new columns to target table we maintain historical information and current
information
What are Semi-additive and factless facts and in which scenario will you use such kinds of fact
tables?
Snapshot facts are semi-additive, while we maintain aggregated facts we go for semi-additive.
EX: Average daily balance
A fact table without numeric fact columns is called factless fact table.
Ex: Promotion Facts
While maintain the promotion values of the transaction (ex: product samples) because this table
doesnt contain any measures.
Why fact table is in normal form?
Basically the fact table consists of the Index keys of the dimension/ook up tables and the measures.
so when ever we have the keys in a table .that itself implies that the table is in the normal form.
What is degenerate dimension table?
Degenerate Dimensions : If a table contains the values, which are neither dimesion nor measures is
called degenerate dimensions.Ex : invoice id,empno
what is junk dimension? what is the difference between junk dimension and degenerated
dimension?
Junk dimension: Grouping of Random flags and text Attributes in a dimension and moving them to a
separate sub dimension.
Degenerate Dimension: Keeping the control information on Fact table ex: Consider a Dimension
table with fields like order number and order line number and have 1:1 relationship with Fact table, In
this case this dimension is removed and the order information will be directly stored in a Fact table
inorder eliminate unneccessary joins while retrieving order information..
4.what is aggregate fact table?
Aggregate table contains the [measure] values ,aggregated /grouped/summed up to some level of
hirarchy.
What is fact less fact table? where you have used it in your project?
Factless table means only the key available in the Fact there is no mesures availalable
Q.What is a dimension table?
Dimension tables contain attributes that describe fact records in the fact table. Dimension tables
contain hierarchies of attributes that aid in summarization. Example: Time Dimension, Product
Dimension
Q.What is Fact table?
A. Fact Table contains the measurements or metrics or facts of business process.
Q. What are conformed dimensions?

A. Dimension tables contain attributes that describe fact records in the fact table. Dimension tables
contain hierarchies of attributes that aid in summarization. Example: Time Dimension.
Q. What is ODS?
A. ODS means Operational Data Store. A collection of operation or bases data that is extracted
from operation databases and standardized, cleansed, consolidated, transformed, and loaded into
an enterprise data architecture. An ODS is used to support data mining of operational data, or as the
store for base data that is summarized for a data warehouse.
Q. What is a lookup table?
A. A lookup table is used to display information from one table based on the value of a foreign-key
field in another table.
Q. What are Aggregate tables?
A. Aggregate table contains the summary of existing warehouse data which is grouped to certain
levels of dimensions. Aggregate table gives better performance for many reporting needs. Aggregate
table contains the [measure] values, aggregated/grouped/summed up to some level of hierarchy.
Q.What is a level of Granularity of a fact table?
A. Level of granularity means level of detail that you put into the fact table in a data warehouse. For
example: Based on design you can decide to put the sales data in each transaction. Now, level of
granularity would mean what detail are you willing to put for each transactional fact. Product sales
with respect to each minute or you want to aggregate it upto minute and put that data.
Q. How are the Dimension tables designed?
A. Most dimension tables are designed using Normalization principles upto 2NF. In some instances
they are further normalized to 3NF.
Q. What are non-additive facts?
A. Non-additive facts are facts that cannot be summed up for any of the dimensions present in the
fact table.
Q. What are slowly changing dimensions?
SCD stands for Slowly changing dimensions. Slowly changing dimensions are of three types
SCD1: Only maintain updated/Current values.
Ex: a customer address modified we update existing record with new address.
SCD2: Maintain historical and current information by using A) Effective Date B) Versions C)
Flags or combination of all three of those.
SCD3: By adding new columns to target table to maintain both historical and current information
Q. What are Semi-additive and factless facts and in which scenario will you use such kinds of
fact tables?
A. Generally most of the Snapshot fact tables are semi-additive.
A fact table without numeric fact columns is called factless fact table.

Q. What is degenerate dimension table?


A. If a table contains the values, which are neither dimesion nor measures is called degenerate
dimensions.Ex : invoice id,EmpId.
Q.What is a junk dimension? what is the difference between junk dimension and degenerated
dimension?
A. Junk dimension: Grouping of Random flags and text Attributes in a dimension and moving them to
a separate sub dimension.
Degenerate Dimension: Keeping the control information on Fact table ex: Consider a Dimension
table with fields like order number and order line number and have 1:1 relationship with Fact table, In
this case this dimension is removed and the order information will be directly stored in a Fact table
inorder eliminate unneccessary joins while retrieving order information.
Q.What is a fact less fact table?
A. Factless table means only the keys are available in the Fact while there are
no measures available.

You might also like