SQL (General) Flashcards
What is SQL?
tructured Query Language (SQL) is the language used in relational database management systems (RDBMS) to query, update, and delete data. SQL is a standard query language for RDBMS. SQL language’s queries are also known as SQL commands or SQL statements.
What are the types of Joins in SQL?
There are four types of joins in SQL.
- INNER JOIN: Returns all rows when there is at least one match in BOTH the tables.
- LEFT JOIN: Returns all rows from the left table, and the matched rows from the right table.
- RIGHT JOIN: Returns all rows from the right table, and the matched rows from the left table.
- FULL JOIN: Returns all rows when there is a match in ONE of the table.
What is the default join in SQL?
The default join in SQL is INNER JOIN.
Describe a LEFT JOIN in SQL?
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name
Describe a RIGHT JOIN in SQL?
The right join returns all the rows in the right table (table2) with the matching ones on the left table (table1).
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
Describe a FULL OUTER JOIN in SQL?
The full join returns all rows from the left table (table1) and from the right table (table2).
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
What is Union?
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default.
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
What is Union All?
The UNION operator is used to combine the result-set of two or more SELECT statements.
UNION ALL is not distinct as the UNION is.
SQL UNION ALL Syntax
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Differentiate Clustered and Non clustered Index in SQL?
A clustered index is one in which the index’s order is arranged according to the physical order of rows in the table. Due to this reason there can only be one clustered index per table, usually this is the primary key.
A non clustered index is one in which the order of index is not in accordance with the physical order of rows in the table.
What is a Composite Key?
A composite key, in the context of relational databases, is a combination of two or more columns in a table that can be used to uniquely identify each row in the table. Uniqueness is only guaranteed when the columns are combined; when taken individually the columns do not guarantee uniqueness.
Explain the difference between Stored Procedure and User Defined Function?
Stored Procedure (mini program):
Stored procedures are reusable code in database which is compiled for first time and its execution plan saved. The compiled code is executed when every time it is called.
Function (computed values):
Function is a database object in SQL Server. Basically it is a set of SQL statements that accepts only input parameters, perform actions and return the result. Function can return only a single value or a table. We can’t use functions to Insert, Update, Delete records in the database table(s). It is compiled every time it is invoked.
What are Cursors and why do we use Cursors in SQL Server?
A SQL cursor is a database object which is used to retrieve data from a result set one row at a time. A SQL cursor is used when the data needs to be updated row by row.
In relational databases, operations are made on a set of rows. For example, a SELECT statement returns a set of rows which is called a result set. Sometimes the application logic needs to work with one row at a time rather than the entire result set at once. This can be done using cursors.
What are triggers and when to use a trigger
A trigger is a special kind of Stored Procedure or stored program that is automatically fired or executed when some event (insert, delete and update) occurs.
If you write a trigger for an insert operation on a table, after firing the trigger, it creates a table named “INSERTED” in memory. Then it does the insert operation and then the statements inside the trigger executes. We can query the “INSERTED” table to manipulate or use the inserted row(s) from the trigger. Similarly, if you write a trigger for a delete operation on a table, it creates a table in memory named “DELETED” and then deletes the row.
We use a trigger when we want some event to happen automatically on certain desirable scenarios.
What are Views in SQL Server
Views are database objects which are like virtual tables that have no physical storage and contains data from one table or multiple tables. A View does not have any physical storage so they do not contain any data. When we update, insert or apply any operation over the View then these operations are applied to the table(s) on which the view was created.
What is the difference between CHAR and VARCHAR datatype?
The CHAR data type:
It is a fixed length data type
Used to store non-Unicode characters
Occupiers 1 byte of space for each character
The VARCHAR data type:
It is a variable length data type
Used to store non-Unicode characters
Occupies 1 byte of space for each character
What is Cast() and Convert() Functions in SQL Server?
Both CAST and CONVERT are functions used to convert one data type to another data type. CAST and CONVERT are often used interchangeably. CAST is an ANSI standard and is available on most SQL platforms whereas CONVERT is a MSSQL specific function.
What is Commit and Rollback Commands in SQL Server?
Commit is used for permanent changes. When we use Commit in any query then the change made by that query will be permanent and visible. We can’t Rollback after the Commit.