Docsity

SQL Statements, Constraints, and Functions, Summaries of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Statements, Constraints, and Functions and more Summaries Database Management Systems (DBMS) in PDF only on Docsity!

SQL Interview Questions

  1. What is SQL? Structured Query Language is a database tool which is used to create and access database to support software application.
  2. What are different types of statements supported by SQL?
  3. What is DBMS? A Database Management System (DBMS) is a program that controls creation, maintenance and use of a database.

DBMS can be termed as File Manager that manages data in a database rather than saving it in file systems.

  1. What is RDBMS? RDBMS stands for Relational Database Management System. RDBMS store the data into the collection of tables, which is related by common fields between the columns of the table. It also provides relational operators to manipulate the data stored into the tables. Example: SQL Server.
  2. Why do we use SQL constraints? Which constraints we can use while creating database in SQL? Constraints are used to set the rules for all records in the table. If any constraints get violated then it can abort the action that caused it. Constraints are defined while creating the database itself with CREATE TABLE statement or even after the table is created once with ALTER TABLE statement. There are 5 major constraints are used in SQL, such as NOT NULL: That indicates that the column must have some value and cannot be left null UNIQUE: This constraint is used to ensure that each row and column has unique value and no value is being repeated in any other row or column PRIMARY KEY: This constraint is used in association with NOT NULL and UNIQUE constraints such as on one or the combination of more than one column to identify the particular record with a unique identity. FOREIGN KEY: It is used to ensure the referential integrity of data in the table and also matches the value in one table with another using Primary Key CHECK: It is used to ensure whether the value in columns fulfills the specified condition
  3. What are different JOINS used in SQL?
  1. What is normalization? Normalization is the process of minimizing redundancy and dependency by organizing fields and table of a database. The main aim of Normalization is to add, delete or modify field that can be made in a single table.
  2. What are all the different normalizations? The normal forms can be divided into 4 forms, and they are explained below -.
  3. First Normal Form (1NF): This should remove all the duplicate columns from the table. Creation of tables for the related data and identification of unique columns.
  4. Second Normal Form (2NF): Meeting all requirements of the first normal form. Placing the subsets of data in separate tables and Creation of relationships between the tables using primary keys. 3.Third Normal Form (3NF): This should meet all requirements of 2NF. Removing the columns which are not dependent on primary key constraints.
  5. Fourth Normal Form (4NF): Meeting all the requirements of third normal form and it should not have multivalued dependencies.
  6. How many Aggregate Functions are available there in SQL? SQL Aggregate Functions calculates values from multiple columns in a table and returns a single value. There are 7 aggregate functions we use in SQL AVG(): Returns the average value from specified columns COUNT(): Returns number of table rows MAX(): Returns largest value among the records MIN(): Returns smallest value among the records SUM(): Returns the sum of specified column values FIRST(): Returns the first value

LAST(): Returns Last value

  1. What is an Index? What are all the different types of indexes? 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. 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.
  2. Clustered Index: 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.
  3. Non-Clustered Index: Non-Clustered Index does not alter the physical order of the table and maintains logical order of data. Each table can have 999 non clustered indexes.
  4. What is SQL Injection? SQL Injection is a type of database attack technique where malicious SQL statements are inserted into an entry field of database such that once it is executed the database is opened for an attacker. This technique is usually used for attacking Data-Driven Applications to have an access to sensitive data and perform administrative tasks on databases. For Example: SELECT column_name(s) FROM table_name WHERE condition;
  5. What is the difference between “Primary Key” and “Unique Key”? 1.We can have only one Primary Key in a table whereas we can have more than one Unique Key in a table.
  6. The Primary Key cannot have a NULL value whereas a Unique Key may have only one null value.
  7. By default, a Primary Key is a Clustered Index whereas by default, a Unique Key is a unique non-clustered index.
  8. A Primary Key supports an Auto Increment value whereas a Unique Key doesn't support an Auto Increment value.

tool for a developer to understand the performance characteristics of a query or stored procedure since the plan is the one that SQL Server will place in its cache and use to execute the stored procedure or query. From within Query Analyzer is an option called "Show Execution Plan" (located on the Query drop-down menu). If this option is turned on it will display query execution plan in separate window when query is ran again.

  1. How to implement one-to-one, one-to-many and many-to-many relationships while designing tables? One-to-One relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships. One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships. Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.
  2. How to select UNIQUE records from a table using a SQL Query?

Using GROUP BY Function

  1. How to delete DUPLICATE records from a table using a SQL Query? Consider the same EMPLOYEE table as source discussed in previous question Using ROWID and ROW_NUMBER Analytic Function

23.How to read TOP 5 records from a table using a SQL query?

Consider below table DEPARTMENTS as the source data

ROWNUM is a “Pseudocolumn” that assigns a number to each row

returned by a query indicating the order in which Oracle selects the

row from a table. The first row selected has a ROWNUM of 1, the

second has 2, and so on.

24. How to read LAST 5 records from a table using a SQL query?

Consider the same DEPARTMENTS table as source discussed in

previous question.

Without using SQL Analytic Functions In order to find the second MAX salary, employee record with MAX salary needs to be eliminated. It can be achieved by using below SQL query.

  1. How to find the employee with third MAX Salary using a SQL query without using Analytic Functions? Consider the same EMPLOYEES table as source discussed in previous question In order to find the third MAX salary, we need to eliminate the top 2 salary records. But we cannot use the same method we used for finding second MAX salary (not a best practice). Imagine if we have to find the fifth MAX salary. We should not be writing a query with four nested sub queries.

In order to fetch the entire employee record with third MAX salary we need to do a self-join on Employee table based on Salary value.

  1. What is Trigger? 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)
  2. What is CHECK Constraint? 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.
  3. What is Database White Box Testing? Database White Box Testing involves Database Consistency and ACID properties Database triggers and logical views Decision Coverage, Condition Coverage, and Statement Coverage Database Tables, Data Model, and Database Schema Referential integrity rules
  4. What are Nested Triggers? Triggers may implement data modification logic by using INSERT, UPDATE, and DELETE statement. These triggers that contain data

modification logic and find other triggers for data modification are called Nested Triggers.

  1. Assume you have the below tables on sessions that users have, and a users table. Write a query to get the active user count of daily cohorts. By definition, daily cohorts are active users from a particular day. First, we can use a subquery to get the sessions of new users by day using an inner join with users. This is to filter for only active users by a particular join date for the cohort. Then we can get a distinct count to return the active user count: