Sql interview questions filetype pdf




















The fields in a view are fields from one or more real tables in the database. Normalization represents the way of organizing structured data in the database efficiently. It includes the creation of tables, establishing relationships between them, and defining rules for those relationships. Inconsistency and redundancy can be kept in check based on these rules, hence, adding flexibility to the database.

Denormalization is the inverse process of normalization, where the normalized schema is converted into a schema that has redundant information. The performance is improved by using redundancy and keeping the redundant data consistent.

The reason for performing denormalization is the overheads produced in the query processor by an over-normalized structure.

Normal Forms are used to eliminate or reduce redundancy in database tables. The different forms are as follows:. As we can observe, the Books Issued field has more than one value per record, and to convert it into 1NF, this has to be resolved into separate individual records for each book issued.

Check the following table in 1NF form -. A relation is in second normal form if it satisfies the conditions for the first normal form and does not contain any partial dependency.

A relation in 2NF has no partial dependency , i. Often, specifying a single column Primary Key is the solution to the problem. Examples -. Example 1 - Consider the above example. As we can observe, the Students Table in the 1NF form has a candidate key in the form of [Student, Address] that can uniquely identify all records in the table. The field Books Issued non-prime attribute depends partially on the Student field.

Hence, the table is not in 2NF. To convert it into the 2nd Normal Form, we will partition the tables into two while specifying a new Primary Key attribute to identify the individual records in the Students table.

The Foreign Key constraint will be set on the other table to ensure referential integrity. Here, WX is the only candidate key and there is no partial dependency, i. A relation is said to be in the third normal form, if it satisfies the conditions for the second normal form and there is no transitive dependency between the non-prime attributes, i. Example 1 - Consider the Students Table in the above example. The field Salutation non-prime attribute , however, depends on the Student Field rather than the candidate key.

Hence, the table is not in 3NF. To convert it into the 3rd Normal Form, we will once again partition the tables into two while specifying a new Foreign Key constraint to identify the salutations for individual records in the Students table. The Primary Key constraint for the same will be set on the Salutations table to identify each record uniquely.

A relation is in Boyce-Codd Normal Form if satisfies the conditions for third normal form and for every functional dependency, Left-Hand-Side is super key. DROP command is used to remove an object from the database. If you drop a table, all the rows in the table are deleted and the table structure is removed from the database. If a table is dropped, all things associated with the tables are dropped as well. This includes - the relationships defined on the table with other tables, the integrity checks and constraints, access privileges and other grants that the table has.

To create and use the table again in its original form, all these relations, checks, constraints, privileges and relationships need to be redefined.

However, if a table is truncated, none of the above problems exist and the table retains its original structure. The DELETE command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified.

But it does not free the space containing the table. An aggregate function performs operations on a collection of values to return a single scalar value. Following are the widely used SQL aggregate functions:. A scalar function returns a single value based on the input value. Following are the widely used SQL scalar functions:. The user-defined functions in SQL are like functions in any other programming language that accept parameters, perform complex calculations, and return a value.

They are written to use the logic repetitively whenever required. There are two types of SQL user-defined functions:. OLTP stands for Online Transaction Processing, is a class of software applications capable of supporting transaction-oriented programs. An essential attribute of an OLTP system is its ability to maintain concurrency. To avoid single points of failure, OLTP systems are often decentralized.

These systems are usually designed for a large number of users who conduct short transactions. Database queries are usually simple, require sub-second response times, and return relatively few records. OLTP stands for Online Transaction Processing , is a class of software applications capable of supporting transaction-oriented programs.

An important attribute of an OLTP system is its ability to maintain concurrency. OLTP systems often follow a decentralized architecture to avoid single points of failure. These systems are generally designed for a large audience of end-users who conduct short transactions. Queries involved in such databases are generally simple, need fast response times, and return relatively few records.

A number of transactions per second acts as an effective measure for such systems. OLAP stands for Online Analytical Processing , a class of software programs that are characterized by the relatively low frequency of online transactions. Queries are often too complex and involve a bunch of aggregations.

For OLAP systems, the effectiveness measure relies highly on response time. Such systems are widely used for data mining or maintaining aggregated, historical data, usually in multi-dimensional schemas. Collation refers to a set of rules that determine how data is sorted and compared. Rules defining the correct character sequence are used to sort the character data.

It incorporates options for specifying case sensitivity, accent marks, kana character types, and character width. Below are the different types of collation sensitivity:. A stored procedure is a subroutine available to applications that access a relational database management system RDBMS. Such procedures are stored in the database data dictionary. The sole disadvantage of stored procedure is that it can be executed nowhere except in the database and occupies more memory in the database server.

It also provides a sense of security and functionality as users who can't access the data directly can be granted access via stored procedures. A stored procedure that calls itself until a boundary condition is reached, is called a recursive stored procedure. This recursive function helps the programmers to deploy the same set of code several times as and when required.

Some SQL programming languages limit the recursion depth to prevent an infinite loop of procedure calls from causing a stack overflow, which slows down the system and may lead to system crashes. Creating empty tables with the same structure can be done smartly by fetching the records of one table into a new table using the INTO operator while fixing a WHERE clause to be false for all records.

Hence, SQL prepares the new table with a duplicate structure to accept the fetched records but since no records get fetched due to the WHERE clause in action, nothing is inserted into the new table. SQL pattern matching provides for pattern search in data if you have no clue as to what that word should be. This kind of SQL query uses wildcards to match a string pattern, rather than writing the exact word. Search a student in your database with first name beginning with the letter K:.

Use the NOT keyword to select records that don't match the pattern. This query returns all students whose first name does not begin with K. This query fetches all students with letter K at the third position in their first name.

It limits the length and position of the matched results. For example -. It was developed to help developers build enterprise-level applications by upholding data integrity by making systems fault-tolerant.

PostgreSQL is therefore an enterprise-level, flexible, robust, open-source, and object-relational DBMS that supports flexible workloads along with handling concurrent users. It has been consistently supported by the global developer community. Due to its fault-tolerant nature, PostgreSQL has gained widespread popularity among developers.

Indexes are the inbuilt functions in PostgreSQL which are used by the queries to perform search more efficiently on a table in the database. Consider that you have a table with thousands of records and you have the below query that only a few records can satisfy the condition, then it will take a lot of time to search and return those rows that abide by this condition as the engine has to perform the search operation on every single to check this condition.

This is undoubtedly inefficient for a system dealing with huge data. Now if this system had an index on the column where we are applying search, it can use an efficient method for identifying matching rows by walking through only a few levels. This is called indexing. The first step of using PostgreSQL is to create a database. Partitioned tables are logical structures that are used for dividing large tables into smaller structures that are called partitions.

This approach is used for effectively increasing the query performance while dealing with large database tables. To create a partition, a key called partition key which is usually a table column or an expression, and a partitioning method needs to be defined. There are three types of inbuilt partitioning methods provided by Postgres:. The type of partition key and the type of method used for partitioning determines how positive the performance and the level of manageability of the partitioned table are.

A token in PostgreSQL is either a keyword, identifier, literal, constant, quotes identifier, or any symbol that has a distinctive personality. They may or may not be separated using a space, newline or a tab. If the tokens are keywords, they are usually commands with useful meanings. Tokens are known as building blocks of any PostgreSQL code. The truncate statement can also be used to reset values of the identity columns along with data cleanup as shown below:.

We can also use the statement for removing data from multiple tables all at once by mentioning the table names separated by comma as shown below:. A sequence is a schema-bound, user-defined object which aids to generate a sequence of integers. This is most commonly used to generate values to identity columns in a table. They are character sequences bound within single quotes.

These are using during data insertion or updation to characters in the database. There are special string constants that are quoted in dollars. They are database transaction properties which are used for guaranteeing data validity in case of errors and failures. MVCC or Multi-version concurrency control is used for avoiding unnecessary database locks when 2 or more requests tries to access or modify the data at the same time.

This ensures that the time lag for a user to log in to the database is avoided. The transactions are recorded when anyone tries to access the content. For more information regarding this, you can refer here.

The command enable-debug is used for enabling the compilation of all libraries and applications. When this is enabled, the system processes get hindered and generally also increases the size of the binary file. Hence, it is not recommended to switch this on in the production environment. This is most commonly used by developers to debug the bugs in their scripts and help them spot the issues. For more information regarding how to debug, you can refer here.

SQL standards state that the following three phenomena should be prevented whilst concurrent transactions. SQL standards define 4 levels of transaction isolations to deal with these phenomena. To tackle these, there are 4 standard isolation levels defined by SQL standards. They are as follows:.

Write Ahead Logging is a feature that increases the database reliability by logging changes before any changes are done to the database.

This ensures that we have enough information when a database crash occurs by helping to pinpoint to what point the work has been complete and gives a starting point from the point where it was discontinued. For more information, you can refer here.

In case our requirement entails just remove the data, then we would need to recreate the table to store data in it.

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. 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. Data Integrity defines the accuracy and consistency of data stored in a database. It can also define integrity constraints to enforce business rules on the data when it is entered into the application or database. Auto increment keyword allows the user to create a unique number to be generated when a new record is inserted into the table. Clustered index is used for easy retrieval of data from the database by altering the way that the records are stored.

Database sorts out rows by the column which is set to be clustered index. A nonclustered index does not alter the way it was stored but creates a complete separate object within the table. It point back to the original table rows after searching. Datawarehouse is a central repository of data from multiple sources of information. Those data are consolidated, transformed and made available for the mining and online processing. Warehouse data have a subset of data called Data Marts.

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. Cross join defines as Cartesian product where number of rows in the first table multiplied by number of rows in the second table.

User defined functions are the functions written to use that logic whenever required. It is not necessary to write the same logic several times. Instead, function can be called or executed whenever needed. Scalar returns unit, variant defined the return clause. Other two types return table as a return.

Collation is defined as set of rules that determine how character data can be sorted and compared. This can be used to compare A and, other language characters and also depends on the width of the characters. Stored procedure can be used as a modular programming — means create once, store and call for several times whenever required. This supports faster execution instead of executing multiple queries.

This reduces network traffic and provides better security to the data. Disadvantage is that it can be executed only in the Database and utilizes more memory in the database server. Online Transaction Processing OLTP manages transaction based applications which can be used for data entry, data retrieval and data processing.

OLTP makes data management simple and efficient. 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. A stored procedure which calls by itself until it reaches some boundary condition.

This recursive function or procedure helps programmers to use the same set of code any number of times. UNION operator is used to combine the results of two tables, and it eliminates duplicate rows from the tables. MINUS operator is used to return rows from the first query but not from the second query.

Matching records of first and second query and other rows from the first query will be displayed as a result set. ALIAS name can be given to a table or column. DROP command removes a table from the database and operation cannot be rolled back. Aggregate functions are used to evaluate mathematical calculation and return single values. This can be calculated from the columns in a table.

Scalar functions return a single value based on the input value. The stuff function deletes a part of the string and then inserts another part into the string starting at a specified position. Here, String1 is the one that would be overwritten. Position indicates the starting location for overwriting the string. Length is the length of the substitute string, and String2 is the string that would overwrite String1.

Views are virtual tables used to limit the tables that we want to display, and these are nothing but the result of a SQL statement that has a name associated with it. Since views are not physically present, they take less space to store. We can create a view-only table for the female employees from the entire employee table.

A stored procedure is a prepared SQL code that can be saved and reused. In other words, we can consider a stored procedure to be a function consisting of many SQL statements to access the database system. We can consolidate several SQL statements into a stored procedure and execute them whenever and wherever required. A stored procedure can be used as a means of modular programming, i.

This also supports faster execution when compared to executing multiple queries. The Join clause is used to combine rows from two or more tables based on a related column between them.

There are various types of Joins that can be used to retrieve data, and it depends upon the relationship between tables. In SQL Server, usernames and passwords are stored in the main database in the sysxlogins table.

Relationships are developed by interlinking the column of one table with the column of another table. There are three different types of relationships which are as follows:. Let us suppose, we have two tables Table A and Table B. When we apply Inner Join on these two tables, we will get only those records that are common to both Table A and Table B.

Output :. After Inner Join, we have only those records where the departments match in both tables. As we can see, the matched departments are Support, Analytics, and Sales. Views Tables It is a virtual table that is extracted from a database. A table is structured with a set number of columns and a boundless number of rows. Views do not hold data themselves. A table contains data and stores the data in databases.

A view is also utilized to query certain information contained in a few distinct tables. A table holds fundamental client information and the cases of a characterized object. In a view, we will get frequently queried information. In a table, changing the information in the database changes the information that appears in the view. A temporary table helps us store and process intermediate results.

These temporary tables are created and can be automatically deleted when they are no longer used. They are very useful in places where we need to store temporary data. OLTP: It stands for Online Transaction Processing, and we can consider it to be a category of software applications that is efficient for supporting transaction-oriented programs. One of the important attributes of the OLTP system is its potential to keep up the consistency.

The OLTP system often follows decentralized planning to keep away from single points of failure. This system is generally designed for a large audience of end-users to perform short transactions. Also, queries involved in such databases are generally simple, need fast response time, and in comparison, return only a few records. So, the number of transactions per second acts as an effective measure for those systems. For OLAP systems, the efficiency of computing depends highly on the response time.

Hence, such systems are generally used for data mining or maintaining aggregated historical data, and they are usually used in multi-dimensional schemas. Self Join in SQL is used for joining a table with itself. Here, depending upon some conditions, each row of the table is joined with itself and with other rows of the table. The Union operator is used to combine the result set of two or more select statements. For example, the first select statement returns the fish shown in Image A, and the second returns the fish shown in Image B.

Then, the Union operator will return the result of the two select statements as shown in Image A U B. Also, if there is a record present in both tables, then we will get only one of them in the final result. The aim of the server agent is to easily implement tasks using a scheduler engine that enables tasks to be performed at scheduled times. It can be referred to as a pointer for a row in the set of rows.

Cursors are extremely useful for database traversal operations like extraction, insertion, and elimination. A magic table can be defined as a provisional logical table that is developed by an SQL Server for tasks such as insert, delete, or update D. L operations. The operations recently performed on the rows are automatically stored in magic tables.

They are not physical tables; they are just temporary internal tables. The Intersect operator helps combine two select statements and returns only those records that are common to both the select statements. So, after we get Table A and Table B over here and if we apply the Intersect operator on these two tables, then we will get only those records that are common to the result of the select statements of these two. We have to copy this data into another table.

Before we go ahead and do that, we would have to create another table that would have the same structure as the above-given table. The values may be numbers, text, or dates. To search for values within a given range of values, the IN condition operator is used.

If we have more than one value to choose from, we use the IN operator. Below are the SQL scripts for generating the sample data:. The SQL query above will delete the rows, where the name fields are duplicated, and it will retain only those unique rows in which the names are unique and the ID fields are the lowest.

That is, rows with IDs 5 and 6 are deleted, whereas rows with IDs 1 and 2 are retained. Consider the below Employee table. Below is a simple query to find out the employee who has the third-highest salary. The white box test method mainly deals with the internal structure of a particular database, where users hide specification details.



0コメント

  • 1000 / 1000