SQL Query

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the Primary key?

A Primary key is a column whose values uniquely identify every row in a table. Primary key values can never be reused.

What is View in SQL?

A View can be defined as a virtual table that contains rows and columns with fields from one or more table.

Different type of JOINS

- INNER JOIN - LEFT JOIN - RIGHT JOIN - FULL JOIN

What are different types of statements supported by SQL?

1. DDL - Used to create tables, views etc 2. DML - These statements are used to manipulate the data in records 3. DCL (Data Control Language)- These statements are used to set privileges such as Grant and Revoke database access permission to the specific user.

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.

What is CTE?

A CTE or common table expression is an expression which contains temporary result set which is defined in a SQL statement.

What is the difference between Cluster and Non cluster Index?

A clustered index reorders the way records in the table are physically stored. There can be only one clustered index per table. It makes data retrieval faster. A non clustered index does not alter the way it was stored but creates a completely separate object within the table. As a result insert and update command will be faster.

What is a Cursor?

A cursor is a database object which is used to manipulate data in a row-to-row manner. Cursor follows steps as given below Declare Cursor Open Cursor Retrieve row from the Cursor Process the row Close Cursor Deallocate Cursor

What is Relationship? How many types of Relationship are there

A relationship is a connection between more than one tables in the database. There are 4 types of relationships 1. One to One Relationship 2. Many to One Relationship 3. Many to Many Relationship 4. One to Many Relationship

What is schema?

A schema is a collection of database objects of a User.

What is Stored Procedures

A stored procedure is a collection of SQL statements which can be used as a function to access the database.

Unique vs Primary Key

A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The primary key cannot contain Null values whereas Unique key can contain Null values.

Define a temp table?

A temp table is a temporary storage structure to store the data temporarily.

What is Transaction ?

A transaction is a sequence of operations performed (using one or more SQL statements) on a database as a single logical unit of work. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database). A database transaction must be atomic, consistent, isolated and durable.

What are the advantages of Views?

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.

What are the advantages and disadvantages of views in a database?

Advantages: 1. Views don't store data in a physical location. 2. The view can be used to hide some of the columns from the table. 3. Views can provide Access Restriction, since data insertion, update and deletion is not possible with the view. Disadvantages: 1. When a table is dropped, associated view become irrelevant. 2. Since the view is created when a query requesting data from view is triggered, its a bit slow. 3. When views are created for large tables, it occupies more memory.

What is an Index?

An Index is a special structure associated with a table speed up the performance of queries. The index can be created on one or more columns of a table.

What is identity in SQL?

An identity column in the SQL automatically generates numeric values. We can define a start and increment value of the identity column.

What is the difference between Having clause and Where clause?

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 behaved like WHERE clause only.

Which TCP/IP port does SQL Server run?

By default SQL Server runs on port 1433.

4 Transaction controls are ?

COMMIT: Save all changes made through the transaction ROLLBACK: Roll backs the transaction such as all changes made by the transaction are reverted back and database remains as before SET TRANSACTION: Set the name of transaction SAVEPOINT: Set the point from where the transaction is to be rolled back

What is Collation?

Collation is a set of rules that check how the data is sorted by comparing it. Such as Character data is stored using correct character sequence along with case sensitivity, type, and accent.

Usage of Constraints

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. 5 major constraints are : - NOT NULL - UNIQUE - PRIMARY KEY - FOREIGN KEY - CHECK

Difference between TRUNCATE, DELETE and DROP commands?

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.

What is Database Black Box Testing?

Database Black Box Testing involves Data Mapping Data stored and retrieved Use of Black Box techniques such as Equivalence Partitioning and Boundary Value Analysis (BVA)

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

What is Database lock

Database locks tells a transaction if the data item in question is currently being used by other transactions.

What do we need to check in Database Testing?

Generally, in Database Testing following thing is need to be tested Database Connectivity Constraint Check Required Application Field and its size Data Retrieval and Processing With DML operations Stored Procedures Functional flow

What is the difference between Local and Global temporary table?

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 disappear when the connection is closed.

State some properties of Relational databases?

In relational databases, each column should have a unique name The sequence of rows and columns in relational databases are insignificant All values are atomic and each row is unique

What is the use of NVL function?

NVL function is used to convert the null value to its actual value.

Inner Join

It is also known as SIMPLE JOIN which returns all rows from BOTH tables when it has at least one column matched SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON column_name1=column_name2;

What do you mean by ROWID?

It's an 18 character long pseudo column attached with each row of a table.

Define UNION, MINUS, UNION ALL, INTERSECT ?

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.

What is a materialized view?

Materialized views are also a view but are disk based. Materialized views get updates on specific duration, base upon the interval specified in the query definition. We can index materialized view.

Does View contain Data?

No, Views are virtual structure.

What is Normalization? How many Normalization forms are there?

Normalization is a process to reduce the data redundancy in the database and avoid insert, update and delete anomalies. There are 5 forms of Normalization First Normal Form (1NF): It removes all duplicate columns from the table. Creates a table for related data and identifies unique column values First Normal Form (2NF): Follows 1NF and creates and places data subsets in an individual table and defines the relationship between tables using the primary key Third Normal Form (3NF): Follows 2NF and removes those columns which are not related through primary key Fourth Normal Form (4NF): Follows 3NF and do not define multi-valued dependencies. 4NF also known as BCNF

What is the difference between clustered and non-clustered indexes?

One table can have only one clustered index but multiple nonclustered indexes. Clustered indexes can be read rapidly rather than non-clustered indexes. Clustered indexes store data physically in the table or view and non-clustered indexes do not store data in table as it has separate structure from data row

Give the order of SQL SELECT?

Order of SQL SELECT clauses is: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Only the SELECT and FROM clause are mandatory.

What do you mean by query optimization?

Query optimization is a process in which a database system compares different query strategies and select the query with the least cost.

What is subquery

Query within another query is called as Subquery

Explain the difference between Rename and Alias?

Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or column.

How to write a query to show the details of a student from Students table whose name starts with K?

SELECT * FROM Student WHERE Student_Name like 'K%'; Here 'like' operator is used for pattern matching

Suppose a Student column has two columns, Name and Marks. How to get name and marks of the top three students.

SELECT Name, Marks FROM Student s1 where 3 <= (SELECT COUNT(*) FROM Students s2 WHERE s1.marks = s2.marks)

List the various privileges that a user can grant to another user?

SELECT, CONNECT, RESOURCES.

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

How we can update the view?

SQL CREATE and REPLACE can be used for updating the view.

Explain the working of SQL Privileges?

SQL GRANT and REVOKE commands are used to implement privileges in SQL multiple user environments. The administrator of the database can grant or revoke privileges to or from users of database object like SELECT, INSERT, UPDATE, DELETE, ALL etc. GRANT Command: This command is used provide database access to user apart from an administrator. REVOKE Command: This command is used provide database deny or remove access to database objects.

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.

What are wild cards used in database for pattern matching ?

SQL Like operator is used for pattern matching. SQL 'Like' command takes more time to process. So before using "like" operator, consider suggestions given below on when and where to use wild card search. 1) Don't overuse wild cards. If another search operator will do, use it instead. 2) When you do use wild cards, try not to use them at the beginning of the search pattern, unless absolutely necessary. Search patterns that begin with wild cards are the slowest to process. 3) Pay careful attention to the placement of the wild card symbols. If they are misplaced, you might not return the data you intended.

What is SQL Sandbox in SQL Server?

SQL Sandbox is the safe place in SQL Server Environment where untrusted scripts are executed. There are 3 types of SQL sandbox, such as Safe Access Sandbox: Here a user can perform SQL operations such as creating stored procedures, triggers etc. but cannot have access to the memory and cannot create files. External Access Sandbox: User can have access to files without having a right to manipulate the memory allocation. Unsafe Access Sandbox: This contains untrusted codes where a user can have access to memory.

What is SQL comments?

SQL comments can be put by two consecutive hyphens (-).

What is the difference between SQL and PL/SQL?

SQL is a structured query language to create and access databases whereas PL/SQL comes with procedural concepts of programming languages.

What are Scalar Functions in SQL?

Scalar Functions are used to return a single value based on the input values. Scalar Functions are as follows UCASE(): Converts the specified field in upper case LCASE(): Converts the specified field in lower case MID(): Extracts and returns character from text field FORMAT(): Specifies the display format LEN(): Specifies the length of text field ROUND(): Rounds up the decimal field value to a number

What is Referential Integrity?

Set of rules that restrict the values of one or more columns of the tables based on the values of the primary key or unique key of the referenced table.

What are the types of locks ?

Shared Lock When a shared lock is applied on data item, other transactions can only read the item, but can't write into it. Exclusive Lock When an exclusive lock is applied on data item, other transactions can't read or write into the data item.

Nested Subquery vs Correlated Subquery?

Subquery within another subquery is called as Nested Subquery. If the output of a subquery is depending on column values of the parent query table then the query is called Correlated Subquery. SELECT adminid(SELEC Firstname+' '+Lastname FROM Employee WHERE empid=emp. adminid)AS EmpAdminId FROM Employee This query gets details of an employee from the Employee table.

What is the difference between DROP and TRUNCATE?

TRUNCATE removes all rows from the table which cannot be retrieved back, DROP removes the entire table from the database and it cannot be retrieved back.

What is the difference between DELETE and TRUNCATE?

The basic difference in both is DELETE is DML command and TRUNCATE is DDL DELETE is used to delete a specific row from the table whereas TRUNCATE is used to remove all rows from the table We can use DELETE with WHERE clause but cannot use TRUNCATE with it

What are Indexes in SQL?

The index can be defined as the way to retrieve the data more quickly. We can define indexes using CREATE statements. Syntax: CREATE INDEX index_name ON table_name (column_name) Further, we can also create Unique Index using following syntax; Syntax: CREATE UNIQUE INDEX index_name ON table_name (column_name)

What is the Cartesian product of table?

The output of Cross Join is called as a Cartesian product. It returns rows combining each row from the first table with each row of the second table. For Example, if we join two tables having 15 and 20 columns the Cartesian product of two tables will be 15×20=300 Rows.

What is table ?

The table is a collection of record and its information at a single view.

How many types of Privileges are available in SQL ?

There are 2 types of privileges used in SQL. System Privilege: System privileges deal with an object of a particular type and specifies the right to perform one or more actions on it which include Admin allows a user to perform administrative tasks, ALTER ANY INDEX, ALTER ANY CACHE GROUP CREATE/ALTER/DELETE TABLE, CREATE/ALTER/DELETE VIEW etc. Object Privilege: This allows to perform actions on an object or object of another user(s) viz. table, view, indexes etc. Some of the object privileges are EXECUTE, INSERT, UPDATE, DELETE, SELECT, FLUSH, LOAD, INDEX, REFERENCES etc.

ow many row comparison operators are used while working with a subquery

There are 3-row comparison operators which are used in subqueries such as IN, ANY and ALL

Usage of HAVING Clause

This clause is used in association with the GROUP BY clause. It is applied to each group of result or the entire result as a single group and much similar as WHERE clause, the only difference is you cannot use it without GROUP BY clause

Properties of Transaction

This is known as ACID Atomicity: Ensures the completeness of all transactions performed. Checks whether every transaction is completed successfully if not then transaction is aborted at the failure point and the previous transaction is rolled back to its initial state as changes undone Consistency: Ensures that all changes made through successful transaction are reflected properly on database Isolation: Ensures that all transactions are performed independently and changes made by one transaction are not reflected on other Durability: Ensures that the changes made in database with committed transactions persist as it is even after system failure

LEFT JOIN (LEFT OUTER JOIN):

This join returns all rows from a LEFT table and its matched rows from a RIGHT table. SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON column_name1=column_name2;

RIGHT JOIN (RIGHT OUTER JOIN)

This joins returns all rows from the RIGHT table and its matched rows from a LEFT table. SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON column_name1=column_name2;

FULL JOIN (FULL OUTER JOIN)

This joins returns all when there is a match either in the RIGHT table or in the LEFT table. SELECT column_name(s) FROM table_name1 FULL OUTER JOIN table_name2 ON column_name1=column_name2;

How do you add a column to a table?

To add another column in the table following command has been used. ALTER TABLE table_name ADD (column_name);

What are triggers?

Triggers in SQL is kind of stored procedures used to create a response to a specific action performed on the table such as Insert, Update or Delete. You can invoke triggers explicitly on the table in the database. Action and Event are two main components of SQL triggers when certain actions are performed the event occurs in response to that action.

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.

Usage of USING clause

USING clause comes in use while working with SQL Joins. It is used to check equality based on columns when tables are joined. It can be used instead ON clause in Joins. Syntax: SELECT column_name(s) FROM table_name JOIN table_name USING (column_name);

How to select random rows from a table?

Using a SAMPLE clause we can select random rows. Example: SELECT * FROM table_name SAMPLE(10);

What are Foreign keys?

When a one table's 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.

Can we rename a column in the output of SQL query?

Yes using the following syntax we can do this. SELECT column_name AS new_name FROM table_name;

Can a View based on another View?

Yes, A View is based on another View.

Is it possible for a table to have more than one foreign key?

Yes, a table can have many foreign keys and only one primary key.


Ensembles d'études connexes

Chapter 30- Patient Management Renal System

View Set

Anatomy physiology Unit 1 and Kahoot

View Set