SQL Basics Flashcards
What are Data definition Language commands?
commands which are used to define the database. CREATE, ALTER, DROP and TRUNCATE are some common DDL commands.
What are Data manipulation language (DML) commands?
commands which are used for manipulation or modification of data.
INSERT, UPDATE, and DELETE are some common DML commands.
What are Data Control Language (DCL) ?
Set of SQL statements used to manage security permissions for users and objects.
DCL includes statements such as GRANT, REVOKE, and DENY.
Give an example of Using OR statement
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID = 2
OR ProductCategoryID = 3 OR ProductCategoryID = 4;
Give an example of using IN statement
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ProductCategoryID IN (2, 3, 4);
What is BETWEEN used for in SQL?
BETWEEN is another shortcut that can be used when filtering for an upper and lower bound for the value instead of using two conditions with the AND operator. The following two queries are equivalent:
SELECT ProductCategoryID AS Category, ProductName
FROM Production.Product
WHERE ListPrice BETWEEN 1.00 AND 10.00;
What are Scalar Sub Queries ?
Scalar subqueries return a single value. Outer queries must process a single result.
What are Multi-valued Subqueries?
Multi-valued subqueries return a result much like a single-column table. Outer queries must be able to process multiple values.
What are self-contained Subqueries?
Self-contained subqueries can be written as stand-alone queries, with no dependencies on the outer query.
A self-contained subquery is processed once when the outer query runs and passes its results to that outer query.
What are Correlated Subqueries?
Correlated subqueries reference one or more columns from the outer query and therefore depend on it.
Correlated subqueries cannot be run separately from the outer query.
Can query results include results of Inner Query ?
One restriction you should keep in mind is that when using a nested query, the results returned to the client can only include columns from the outer query.
So if you need to return columns from both tables, you should write the query using a JOIN.
What is Scalar Function ?
Operate on a single row and return a single value.
Functions like DAY(),YEAR(),UPPER(),SQRT() are scalar
What are Logical Function ?
Compare Multiple values to determine a Single Output
What are Ranking Function?
Operate on a partition (set) of rows
What is Rowset Function ?
Return a virtual table that can be used in a FROM clause in a SQL statements
What is Aggregate Function ?
Take one or more input values, return a single summarizing value
What are common types of Error in MSSQL?
Syntax errors, Data type errors, Permission errors, Locking errors, Dead Lock errors
What is a dead lock error in MSSQL?
A deadlock error in MSSQL occurs when two or more transactions are trying to access the same resources in a circular fashion. This can cause a situation where neither transaction can complete, and SQL Server is forced to intervene to break the deadlock.
How to reduce the likelihood of dead locks occuring ?
Use explicit locks whenever possible. This will help to ensure that transactions are only accessing the resources they need.
Use the NOLOCK hint when possible. This hint tells SQL Server to not take any locks on the data being read, which can help to prevent deadlocks.
How to trouble shoot dead locks?
The error message will usually provide some information about the resources that were involved in the deadlock.
You can use the SQL Server Management Studio to view the current deadlock graph. This graph will show you the transactions that were involved in the deadlock, as well as the resources that they were trying to access.
You can use the DBCC TRACEON (3604) command to enable deadlock logging. This will cause SQL Server to log all deadlocks that occur. You can then use these logs to troubleshoot deadlock problems.
Once you have identified the cause of the deadlock, you can take steps to address the problem. This may involve changing the way that your application accesses data, or it may involve changing the way that SQL Server manages locks.
How do you troubleshoot SQL errors?
Check the permissions on your tables and columns.
Use the SQL Server Profiler to trace your queries.
Use the SQL Server Error Log to view recent errors.
Consult the SQL Server documentation for more information about errors.
What is Data Integrity?
Data Integrity refers to the consistency and maintenance of the data through the life cycle of the database.
In a database, data integrity can be ensured through the implementation of Integrity
Constraints in a table.
Integrity constraints help apply business rules to the database tables.
The constraints can either be at a column level or a table level.
EXISTS()
Returns TRUE if a subquery contains any rows. subquery
Is a restricted SELECT statement. The INTO keyword is not allowed.
First Normalization rule
All attributes should be atomic