{"id":13603479,"url":"https://github.com/xoraus/CrackingTheSQLInterview","last_synced_at":"2025-04-11T19:32:09.195Z","repository":{"id":45804239,"uuid":"203422079","full_name":"xoraus/CrackingTheSQLInterview","owner":"xoraus","description":"DBMS Concepts, SQL Queries \u0026 Schema Design for your Interviews.","archived":false,"fork":false,"pushed_at":"2022-12-15T16:08:30.000Z","size":5724,"stargazers_count":533,"open_issues_count":0,"forks_count":139,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-02T19:31:38.252Z","etag":null,"topics":["database","interview","interview-preparation","interview-questions","mysql","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xoraus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-20T17:21:41.000Z","updated_at":"2024-07-30T03:51:25.000Z","dependencies_parsed_at":"2022-07-17T11:47:38.041Z","dependency_job_id":null,"html_url":"https://github.com/xoraus/CrackingTheSQLInterview","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoraus%2FCrackingTheSQLInterview","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoraus%2FCrackingTheSQLInterview/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoraus%2FCrackingTheSQLInterview/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xoraus%2FCrackingTheSQLInterview/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xoraus","download_url":"https://codeload.github.com/xoraus/CrackingTheSQLInterview/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223476820,"owners_count":17151524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","interview","interview-preparation","interview-questions","mysql","sql"],"created_at":"2024-08-01T19:00:23.162Z","updated_at":"2024-11-07T07:31:06.249Z","avatar_url":"https://github.com/xoraus.png","language":null,"readme":"# **Cracking the SQL Interview**\n\n## A comprehensive guide before your next interview\n\n**1. SQL was developed as an integral part of**\n\nSQL - Structured Query Language is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS).\n\nAlthough SQL is an ANSI (American National Standards Institute) standard, there are different versions of the SQL language.\n\nHowever, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.\n\nNote: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!\n\nWhat Can SQL do?\n\n-   SQL can execute queries against a database\n-   SQL can retrieve data from a database\n-   SQL can insert records in a database\n-   SQL can update records in a database\n-   SQL can delete records from a database\n-   SQL can create new databases\n-   SQL can create new tables in a database\n-   SQL can create stored procedures in a database\n-   SQL can create views in a database\n-   SQL can set permissions on tables, procedures, and views\n\n**2. What does SQL stand for?**\n\nSQL stands for Structured Query Language.\nSQL lets you access and manipulate databases.\nSQL is an ANSI (American National Standards Institute) standard.\n\n**3. SQL is (referring to it as a Programming Language)**\n\n**SQL is a declarative language in which the expected result or operation is given without the specific details about how to accomplish the task.** The steps required to execute SQL statements are handled transparently by the SQL database. Sometimes **SQL is characterized as non-procedural** because procedural languages generally require the details of the operations to be specified, such as opening and closing tables, loading and searching indexes, or flushing buffers and writing data to filesystems. Therefore, SQL is considered to be designed at a higher conceptual level of operation than procedural languages because the lower level logical and physical operations aren't specified and are determined by the SQL engine or server process that executes it.\n\n**4. Which of the following is NOT a SQL command? (SELECT, REMOVE, UPDATE, INSERT)**\n\nREMOVE is not a valid SQL command.  \n\n**Some of The Most Important SQL Commands**\n\n```sql\nSELECT - extracts data from a database\nUPDATE - updates data in a database\nDELETE - deletes data from a database\nINSERT INTO - inserts new data into a database\nCREATE DATABASE - creates a new database\nALTER DATABASE - modifies a database\nCREATE TABLE - creates a new table\nALTER TABLE - modifies a table\nDROP TABLE - deletes a table\nCREATE INDEX - creates an index (search key)\nDROP INDEX - deletes an index\n```\n\n\n**5. What is MySQL?**\n\nMySQL is the most popular Open Source SQL database management system developed, distributed, and supported by Oracle Corporation.\n\n**Another common SQL interview question regarding MySQL may come in a different form**\n\n“**What is the difference between SQL and MySQL?**” or **Difference between SQL and MySQL:**\n\nSQL is a structured query language that is used for manipulating and accessing the relational database, on the other hand, MySQL itself is a relational database that uses SQL as the standard database language.\n\n\n**6. What are some properties of PL/SQL?**\n\nPL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL. PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and Java.\n\nAnother common SQL interview question regarding PL/SQL may come in a different form:\n\n##### “What is the difference between SQL and PL/SQL? or Difference between SQL and PL/SQL:\n\nSQL is a Structured Query Language used to issue a single query or execute a single insert/update/delete.\n\n- PL-SQL is a programming language SQL, used to write full programs using variables, loops,operators etc. to carry out multiple selects/inserts/updates/deletes.\n\n- SQL may be considered as the source of data for our reports, web pages and screens.\n\n- PL/SQL can be considered as the application language similar to Java or PHP. It might be the language used to build, format and display those reports, web pages and screens.\n\n- SQL is a data oriented language used to select and manipulate sets of data.\n\n- PL/SQL is a procedural language used to create applications.\n\n#### **SQL vs. PL-SQL**\n\n- SQL is used to write queries, DDL and DML statements.\n- PL/SQL is used to write program blocks, functions, procedures triggers,and packages.\n- SQL is executed one statement at a time.\n- PL/SQL is executed as a block of code.\n- SQL is declarative, i.e., it tells the database what to do but not how to do it. Whereas, PL/SQL is procedural, i.e., it tells the database how to do things.\n- SQL can be embedded within a PL/SQL program. But PL/SQL cant be embedded within a SQL statement.\n\n**7. What are the possible values for the BOOLEAN data field in MySQL?**\n\nMySQL uses TINYINT(1) data type to represent boolean values. A value of zero is considered false . Non-zero values are considered true .\n\n**8. What data type would you choose if you wanted to store the distance (rounded to the nearest mile)?**\n\n```sql\nINTEGER (or INT )   \n```\n\n**9. Which are valid SQL keywords (statements \u0026 clauses)**\n\n- SELECT - extracts data from a database\n- FROM - clause is used to specify the tables to extract data from\n- WHERE - clause is used to extract only those records that fulfill a specified condition.\n- GROUP BY - is often used with aggregate functions (COUNT , MAX , MIN , SUM , AVG ) to group the result-set by one or more columns.\n- HAVING - clause was added to SQL because the WHERE keyword could not be used with aggregate functions.\n- ORDER BY - keyword is used to sort the result-set in ascending or descending order.\n- UPDATE - updates data in a database\n- DELETE - deletes data from a database\n- INSERT INTO - inserts new data into a database\n- CREATE DATABASE - creates a new database\n- ALTER DATABASE - modifies a database\n- CREATE TABLE - creates a new table\n- ALTER TABLE - modifies a table\n- DROP TABLE - deletes a table\n- CREATE INDEX - creates an index (search key)\n- DROP INDEX - deletes an index\n\n\n**10. Which of the following are valid SQL comments?**\n\nComments are used to explain sections of SQL statements, or to prevent execution of SQL statements.\n\nSingle line comments start with -- .\n\nAny text between -- and the end of the line will be ignored (will not be executed).\n\nThe following example uses a single-line comment as an explanation:\n\n```sql\n--Select all:\nSELECT * FROM Customers;\n```\n\nMulti-line comments start with /* and end with */ .\n\nAny text between /* and */ will be ignored.\n\nThe following example uses a multi-line comment as an explanation:\n\n```sql\n/*Select all the columns\nof all the records\nin the Customers table:*/\nSELECT * FROM Customers;\n```\n\nThe following example uses a multi-line comment to ignore many statements:\n\n```sql\n/*SELECT * FROM Customers;\nSELECT * FROM Products;*/\nSELECT * FROM Suppliers;\n```\n\n**11. Which SQL statement is used to extract data from a database?**\n\nA SELECT statement retrieves zero or more rows from one or more database tables or database views.\n\nAs SQL is a declarative programming language, SELECT queries specify a result set, but do not specify how to calculate it.\n\nThe SELECT statement has many optional clauses:\n\n- **WHERE** specifies which rows to retrieve.\n\n- **GROUP BY** groups rows sharing a property so that an aggregate function can be applied to each group.\n\n- **HAVING** selects among the groups defined by the GROUP BY clause.\n\n- **ORDER BY** specifies an order in which to return the rows.\n\n- **AS** provides an alias which can be used to temporarily rename tables or columns.  \n\n**12. How to select all records from the table 'Products'?**\n\nSELECT * FROM Products;\n\nIf you want to select all the fields available in the table, use the * syntax as this:\n\nSELECT * FROM Products;\n\n**13. Can we rename a column in the output of SQL query?**\n\nYes, using AS .\n\nSQL aliases are used to give a column in a table, a temporary name.\n\nAliases are often used to make column names more readable.\n\nAn alias only exists for the duration of the query.\n\nAlias Column Syntax:\n\n-   SELECT column_name AS alias_name\n-   FROM table_name;\n\n**14. With SQL, how do you select a column named \"FirstName\" from a table named \"Customers\"?**\n\nSELECT FirstName FROM Customers;\n\nThe SELECT statement is used to select data from a database.\n\nThe data returned is stored in a result table, called the result-set.\n\n**SELECT Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name;\n```\n\nHere, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:\n\nSELECT * FROM table_name;\n\n**15. What does the SQL FROM clause do?**\n\nThe SQL FROM clause is used to list the tables and any joins required for the SQL statement.\n\n**16. Which SQL statement is used to return only different values?**\n\nThe SELECT DISTINCT statement is used to return only distinct (different) values.\n\nInside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.\n\n**SELECT DISTINCT Syntax**  \n\n```sql\nSELECT DISTINCT column1, column2, ...\nFROM table_name;\n```\n\n\n**17. Consider the following schema ADDRESSES (id, street_name, number, city, state) Which of the following query would display the distinct cities in the ADDRESSES table?**\n\nSELECT DISTINCT city FROM addresses;\n\n(same theory applies as for the previous question)\n\n**18. With SQL, how do you select all the records from a table named \"Customers\" where the value of the column \"FirstName\" is \"John\"?**\n\nSELECT * FROM Customers WHERE FirstName='John';\n\nThe WHERE clause is used to filter records.\n\nThe WHERE clause is used to extract only those records that fulfill a specified condition.\n\nSQL requires single quotes around text values (most database systems will also allow double quotes).\n\n**19. The OR operator displays a record if ANY conditions listed are true. The AND operator displays a record if ALL of the conditions listed are true.**\n\nThe WHERE clause can be combined with AND , OR , and NOT operators.\n\nThe AND and OR operators are used to filter records based on more than one condition:\n\nThe AND operator displays a record if all the conditions separated by AND are TRUE .\n\nThe OR operator displays a record if any of the conditions separated by OR are TRUE .\n\nThe NOT operator displays a record if the condition(s) is NOT TRUE .\n\n**AND Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name\nWHERE condition1 AND condition2 AND condition3 …;\n```\n\n**OR Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name\nWHERE condition1 OR condition2 OR condition3 ...;\n```\n\n**NOT Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name\nWHERE NOT condition;\n```\n\n\n**20. Which of the following SQL statements has correct syntax?**\n\nSELECT * FROM Table1 WHERE Column1 \u003e= 100\n\nSQL Comparison Operators\n\n- '=' Equal to\n\n- '\u003e' Greater than\n\n- '\u003c' Less than\n\n- '\u003e=' Greater than or equal to\n\n- '\u003c=' Less than or equal to\n\n- '≠' Not equal to\n\n**21. With SQL, how do you select all the records from a table named \"Customers\" where the \"FirstName\" is \"John\" and the \"LastName\" is \"Jackson\"?**\n\n```sql\nSELECT * FROM Customers WHERE FirstName='John' AND LastName='Jackson'\n```\n\nSame answers as for the previous 2 questions.\n\nYou must use the AND operator that displays a record if all the conditions separated by AND are TRUE and the = (‘equal’) comparison operator.\n\n**22. How to select random 10 rows from a table?**\n\nThe easiest way to generate random rows in MySQL is to use the ORDER BY RAND() clause.\n\n```sql\nSELECT * FROM tbl ORDER BY RAND() LIMIT 10;\n```\n\nThis can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, MySQL need to assign random number to each row and then sort them.\n\nEven if you want only 10 random rows from a set of 100k rows, MySQL need to sort all the 100k rows and then, extract only 10 of them.\n\n**23. Examine the following code. What will the value of price be if the statement finds a NULL value? SELECT name, ISNULL(price, 50) FROM PRODUCTS**\n\nWhat is a NULL Value?\n\nA field with a NULL value is a field with no value.\n\nIf a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.\n\nNote: It is very important to understand that a NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!\n\n**How can you return a default value for a NULL?**\n\n**MySQL**\n\nThe MySQL IFNULL() function lets you return an alternative value if an expression is NULL:\n\n```sql\nSELECT ProductName, UnitPrice * (UnitsInStock + IFNULL(UnitsOnOrder, 0))\nFROM Products\n```\n\nor we can use the COALESCE() function, like this:\n\n```sql\nSELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))\nFROM Products\n```\n\n**SQL Server**\n\nThe **SQL Server** ISNULL() function lets you return an alternative value when an expression is NULL :\n\n-   SELECT ProductName, UnitPrice * (UnitsInStock + ISNULL(UnitsOnOrder, 0))\n-   FROM Products\n\n**MS Access**\n\nThe MS Access IsNull() function returns TRUE (-1) if the expression is a null value, otherwise FALSE (0) :\n\n-   SELECT ProductName, UnitPrice * (UnitsInStock + IIF(IsNull(UnitsOnOrder), 0, UnitsOnOrder))\n-   FROM Products\n\n**Oracle**\n\nThe Oracle NVL() function achieves the same result:\n\n-   SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOnOrder, 0))\n-   FROM Products  \n\n**24. Which operator is used to search for a specified text pattern in a column?**\n\nThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column.\n\nThere are two wildcards used in conjunction with the LIKE operator:\n\n% - The percent sign represents zero, one, or multiple characters\n\n_ - The underscore represents a single character\n\nExamples:\n\n-   WHERE CustomerName LIKE 'a%' -- Finds any values that starts with  \"a\"\n-   WHERE CustomerName LIKE '%a' -- Finds any values that ends with  \"a\"\n-   WHERE CustomerName LIKE '%or%' -- Finds any values that have \"or\"  in any position\n-   WHERE CustomerName LIKE '_r%' -- Finds any values that have \"r\"  in the second position\n-   WHERE CustomerName LIKE 'a_%_%' -- Finds any values that starts with  \"a\"  and are at least 3 characters in length\n-   WHERE ContactName LIKE 'a%o' -- Finds any values that starts with  \"a\"  and ends with  \"o\"\n\n**25. How to write a query to show the details of a student from Students table whose FirstName starts with 'K'?**\n\nSELECT * FROM Students WHERE FirstName LIKE 'K%'.\n\nExplanation from previous question applies.\n\n**26. Which operator is used to select values within a range?**\n\nThe BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.\n\nThe BETWEEN operator is inclusive: begin and end values are included.\n\n**BETWEEN Syntax**\n\n```sql\nSELECT column_name(s)\nFROM table_name\nWHERE column_name BETWEEN value1 AND value2;\n```\n\n**27. Which of the following SQL statements is correct?**\n\nSELECT * FROM Sales WHERE Date BETWEEN '01/12/2017' AND '01/01/2018'  \n\nExplanation from previous question applies.\n\n**28. With SQL, how do you select all the records from a table named \"Customers\" where the \"LastName\" is alphabetically between (and including) \"Brooks\" and \"Gray\"?**\n\n```sql\nSELECT * FROM Customers WHERE LastName BETWEEN 'Brooks' AND 'Gray'  \n```\n\nExplanation from previous question applies.  \n\n**29. The 'IN' SQL keyword…**\n\nThe IN operator allows you to specify multiple values in a WHERE clause.\n\nThe IN operator is a shorthand for multiple OR conditions.\n\n**IN Syntax**\n\n```sql\nSELECT column_name(s)\nFROM table_name\nWHERE column_name IN (value1, value2, ...);\n```\n\nor:\n\n```sql\nSELECT column_name(s)\nFROM table_name\nWHERE column_name IN (SELECT STATEMENT); --subquery\n```\n\n**30. What does UPPER function do?**\n\nThe UPPER() function converts a string to upper-case.\n\n**31. What function to use to round a number to the smallest integer value that is greater than or equal to a number?**\n\nThe CEILING() function returns the smallest integer value that is greater than or equal to the specified number.\n\nThe CEIL() function is a synonym for the CEILING() function and also returns the smallest integer value that is greater than or equal to a number.\n\nExample:\n\nSELECT CEILING(25.50); -- returns 26  \n\n**32. How to get current date in MySQL (without time)?**\n\nThe CURDATE() function returns the current date. This function returns the current date as a YYYY-MM-DD format if used in a string context, and as a YYYYMMDD format if used in a numeric context.\n\nThe CURRENT_DATE() function is a synonym for the CURDATE() function.\n\n**33. Which SQL keyword is used to retrieve a maximum value?**\n\nThe MAX() function returns the largest value of the selected column.\n\n**MAX() Syntax**\n\n```sql\nSELECT MAX(column_name)\nFROM table_name\nWHERE condition;\n```\n\n**34. Which SQL functions is used to count the number of results?**\n\nThe COUNT() function returns the number of rows that matches a specified criteria.\n\n**COUNT() Syntax**\n\n```sql\nSELECT COUNT(column_name)\nFROM table_name\nWHERE condition;\n```\n\n\n**35. Which of the following are Aggregate Functions?**\n\nSQL Aggregate functions are:\n\n- **COUNT** counts how many rows are in a particular column.\n\n- **SUM** adds together all the values in a particular column.\n\n- **MIN** and **MAX** return the lowest and highest values in a particular column, respectively.\n\n- **AVG** calculates the average of a group of selected values.\n\n**36. With SQL, how can you return the number of records in the \"Customers\" table?**\n\n**SELECT COUNT(*) FROM Customers**\n\nSame answer as for the previous question.\n\n**37. Which 2 SQL keywords specify the sorting direction of the result set retrieved with ORDER BY clause?**\n\n**ASC | DESC**\n\nThe ORDER BY keyword is used to sort the result-set in ascending or descending order.\n\n**ORDER BY Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name\nORDER BY column1, column2, ... ASC|DESC;\n```\n\n\n**38. If you don't specify ASC or DESC for an ORDER BY clause, the following is used by default:**\n\nThe ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.\n\n**39. Suppose a Students table has two columns, name and mark. How to get name and mark of top three students?**\n\nSELECT name, mark FROM Students ORDER BY mark DESC LIMIT 3;  \n\nYou have to use ORDER BY to order students by their marks and then select just the first 3 records.\n\nThe ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.\n\n**The SQL SELECT TOP Clause**\n\nThe **SELECT TOP** clause is used to specify the number of records to return.\n\nThe SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact on performance.\n\nNot all database systems support the SELECT TOP clause.**MySQL supports the** LIMIT clause to select a limited number of records, while **Oracle uses** ROWNUM .\n\n**40. With SQL, how can you return all the records from a table named \"Customers\" sorted descending by \"FirstName\"?**\n\n```sql\nSELECT * FROM Customers ORDER BY FirstName DESC;\n```\n\nSame answers as for the previous 2 questions apply.\n\n**41. Which of the following SQL statements is correct?**\n\n_SELECT name, COUNT(name) FROM customers GROUP BY name_\n\nSQL aggregate functions like COUNT , AVG , and SUM have something in common: they all aggregate across the entire table. But what if you want to aggregate only part of a table? For example, you might want to count the customers having the same name. In situations like this, you’d need to use the GROUP BY clause. GROUP BY allows you to separate data into groups, which can be aggregated independently of one another.\n\n**42. What is the difference between HAVING clause and WHERE clause?**\n\nBoth specify a search condition but HAVING clause is used only with the SELECT statement and typically used with GROUP BY clause.\n\nIf GROUP BY clause is not used then HAVING behaves like WHERE clause only.\n\nHere are some other differences:\n\n- HAVING filters records that work on summarized GROUP BY results.\n\n- HAVING applies to summarized group records, whereas WHERE applies to individual records.\n\n- Only the groups that meet the HAVING criteria will be returned.\n\n- HAVING requires that a GROUP BY clause is present.\n\n- WHERE and HAVING can be in the same query.\n\n**43. What is JOIN used for?**\n\nA JOIN clause is used to combine rows from two or more tables, based on a related column between them.\n\n**44. What is the most common type of join?**\n\nThe most common type of join used in day to day queries is INNER JOIN .\n\n**45. What are different JOINS used in SQL?**\n\nThere are several types of joins:\n\n**SQL INNER JOIN Keyword**\n\nThe INNER JOIN keyword selects records that have matching values in both tables.\n\n**SQL LEFT JOIN Keyword**\n\nThe LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.\n\n**SQL RIGHT JOIN Keyword**\n\nThe RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.\n\n**SQL FULL OUTER JOIN Keyword**\n\nThe FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records.\n\nNote: FULL OUTER JOIN can potentially return very large result-sets!\n\n**SQL Self JOIN**\n\nA self JOIN is a regular join, but the table is joined with itself.\n\n**SQL CROSS JOIN**\n\nThe SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN .This kind of result is called as Cartesian Product.\n\nIf WHERE clause is used with CROSS JOIN , it functions like an INNER JOIN .\n\n**46. Which of the following is NOT TRUE about the ON clause?**\n\nON clause is used to specify conditions or specify columns to JOIN . ON clause makes the query easy to understand. ON clause allows 3 way (or more) joins.\n\n**47. Assume that Table A is joined to Table B. An inner join:**\n\n**Displays rows (records) only when the values of the Key in table A and the foreign key in table B are equal.**\n\nThe INNER JOIN keyword selects records that have matching values in both tables.\n\n**48. Can you join 3 tables together with inner join:**\n\nYes. You can join multiple tables with inner join.\n\nFor example, for a Faculty table the lookup tables might be Division, with DivisionID as the PK, Country, with CountryID as the PK, and Nationality, with NationalityID as the PK. To join Faculty to the Division, Country, and Nationality tables, the fields DivisionID, CountryID and NationalityID would need to be foreign keys in the Faculty table.  \n\nThe SQL to join them would then be:\n\n```sql\nSELECT \u003cfieldlist\u003e FROM Faculty AS f\nINNER JOIN Division AS d ON d.FacultyID = f.FacultyID\nINNER JOIN Country AS c ON c.FacultyID = f.FacultyID\nINNER JOIN Nationality AS n ON n.FacultyID = f.FacultyID\n```\n\n\n**49. Can you join a table to itself?**\n\nYes. The operation is called self join.\n\n**Self JOIN Syntax**\n\n```sql\nSELECT column_name(s)\nFROM table1 T1, table1 T2\nWHERE condition;\n```\n\n**50. Which of the following is true about Cartesian Products?**\n\n**A Cartesian product is formed when a join condition is omitted.**\n\nThe SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN .This kind of result is called as Cartesian Product.\n\nIf WHERE clause is used with CROSS JOIN , it functions like an INNER JOIN .\n\n**CROSS JOIN Syntax**  \n\n```sql\nSELECT *\nFROM table1\nCROSS JOIN table2;\n```\n\n**51. In relational algebra the INTERSECTION of two sets (set A and Set B) corresponds to**\n\n**A AND B**\n\nThe SQL INTERSECT clause/operator is used to combine two SELECT statements, but returns rows only from the first SELECT statement that are identical to a row in the second SELECT statement. This means INTERSECT returns only common rows returned by the two SELECT statements.\n\n**INTERSECT Syntax**\n\n```sql\nSELECT column1 [, column2 ]\nFROM table1 [, table2 ]\n[WHERE condition]\nINTERSECT\nSELECT column1 [, column2 ]\nFROM table1 [, table2 ]\n[WHERE condition]\n```\n\n\n**52. In relational algebra the UNION of two sets (set A and Set B) corresponds to**\n\n**A OR B**\n\nThe SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows.\n\nTo use this UNION clause, each SELECT statement must have\n\nThe same number of columns selected\n\nThe same number of column expressions\n\nThe same data type and\n\nHave them in the same order\n\nBut they need not have to be in the same length.\n\n**Union Syntax**\n\n```sql\nSELECT column1 [, column2 ]\nFROM table1 [, table2 ]\n[WHERE condition]\nUNION\nSELECT column1 [, column2 ]\nFROM table1 [, table2 ]\n[WHERE condition]\n```\n\n**53. What is the difference between UNION and UNION ALL?**\n\n**UNION** – returns all distinct rows selected by either query\n\n**UNION ALL** – returns all rows selected by either query, including all duplicates.\n\n**54. Having a list of Customer Names that searched for product 'X' and a list of customer Names that bought the product 'X'. What set operator would you use to get only those who are interested but did not bought product 'X' yet?**\n\n**MINUS**\n\nThe **SQL** MINUS operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The MINUS operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.\n\n**MINUS Syntax**\n\n```sql\nSELECT expression1, expression2, ... expression_n\nFROM tables\n[WHERE conditions]\nMINUS\nSELECT expression1, expression2, ... expression_n\nFROM tables\n[WHERE conditions];\n```\n\n\n**55. One (or more) select statement whose return values are used in filtering conditions of the main query is called**\n\n**Subquery.**\n\nA subquery is a SQL query nested inside a main query.\n\nA subquery may occur in :\n\n```sql\nA SELECT clause\nA FROM clause\nA WHERE clause\n```\n\nA subquery is usually added within the WHERE clause of another SQL SELECT statement.\n\nYou can use the comparison operators, such as \u003e , \u003c , or = . The comparison operator can also be a multiple-row operator, such as IN , ANY , or ALL .\n\nA subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer query or outer select.\n\nThe inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.\n\n**56. Subqueries can be nested in...**\n\nThe subquery can be nested inside a SELECT , INSERT , UPDATE , or DELETE statement or inside another subquery.  \n\n**57. What row comparison operators can be used with a subquery?**\n\nYou can use the comparison operators, such as \u003e , \u003c , or = . The comparison operator can also be a multiple-row operator, such as IN , ANY , or ALL .\n\n**58. A subquery that is used inside a subquery is called a**\n\nSubquery within another subquery is called as **Nested Subquery**.\n\n**59. A subquery that uses a correlation name from the outer query is called a**\n\nIf the output of a subquery is depending on column values of the parent query table then the query is called **Correlated Subquery**.\n\n**60. What is Case Function?**\n\nThe CASE function lets you evaluate conditions and return a value when the first condition is met (like an IF-THEN-ELSE statement).\n\n**CASE Syntax**\n\n```sql\nCASE expression\nWHEN condition1 THEN result1\nWHEN condition2 THEN result2\n...\nWHEN conditionN THEN resultN\nELSE result\nEND  \n```\n\n**61. What are different types of statements supported by SQL?**\n\n**DDL** (Data Definition Language): It is used to define the database structure such as tables. It includes three statements such as Create, Alter, and Drop.\n\n**DML** (Data Manipulation Language): These statements are used to manipulate the data in records. Commonly used DML statements are Select, Insert, Update, and Delete.\n\nNote: Some people prefer to assign the SELECT statement to a category of its own called: DQL. Data Query Language.\n\n**DCL** (Data Control Language): These statements are used to set privileges such as Grant and Revoke database access permission to the specific user.\n\n**62. Which of the following SQL statements are DDL**\n\n**DDL statements include:**\n\n- ALTER Statements\n\n- CREATE Statements\n\n- DROP Statements\n\n- TRUNCATE TABLE\n\n**63. DML includes the following SQL statements**\n\n**DML statements are:**\n\n```sql\nSELECT\nINSERT\nUPDATE\nDELETE\n```\n\n\n**64. Grant and Revoke commands are under**\n\n**DCL (Data Control Language)**\n\nGRANT : Used to provide any user access privileges or other privileges for the database.\n\nREVOKE : Used to take back permissions from any user.\n\n**65. Which of the following is not a DML statement?**\n\nSame answer as for the previous questions.\n\nCOMMIT is used to persist changes performed during a TRANSACTION.\n\n**66. Which SQL statement is used to insert new data in a database?**\n\nThe INSERT INTO statement is used to insert new records in a table.\n\n**67. When inserting data in a table do you have to specify the list of columns you are inserting values for?**\n\n**INSERT INTO Syntax**\n\nIt is possible to write the INSERT INTO statement in two ways.\n\nThe first way specifies both the column names and the values to be inserted:\n\n```sql\nINSERT INTO table_name (column1, column2, column3, ...)\nVALUES (value1, value2, value3, ...);\n```\n\nIf you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows:\n\n```sql\nINSERT INTO table_name\nVALUES (value1, value2, value3, ...);\n```\n\n**68. With SQL, how can you insert a new record into the \"Customers\" table?**\n\nSame answer as for the previous question.\n\n**69. With SQL, how can you insert \"Hawkins\" as the \"LastName\" in the \"Customers\" table?**\n\nSame answer as for the previous question.\n\n**70. How do you create a temporary table in MySQL?**\n\nTo create a temporary table, you just need to add the TEMPORARY keyword to the CREATE TABLE statement.\n\nExample:\n\n```sql\nCREATE TEMPORARY TABLE top10customers\nSELECT customer.fname, customer.lname\nFROM customers\n```\n\n-   /* all the conditions to fecth the top 10 customers */\n\n**71. Which SQL statement is used to update data in a database?**\n\nThe UPDATE statement is used to modify the existing records in a table.\n\n**UPDATE Syntax**\n\n```\n-   UPDATE table_name\n-   SET column1 = value1, column2 = value2, ...\n-   WHERE condition;\n```\n\n**72. What is the keyword is used in an UPDATE query to modify the existing value?**\n\nThe answer is SET .\n\n**UPDATE Syntax**\n\n```sql\nUPDATE table_name\nSET column1 = value1, column2 = value2, ...\nWHERE condition;\n```\n\n**73. How can you change \"Jackson\" into \"Hawkins\" in the \"LastName\" column in the Customer table?**\n\n```sql\nUPDATE Customers SET LastName='Hawkins' WHERE LastName='Jackson'  \n```\n\nSame explanation as for the previous question.\n\n**74. Which SQL statement is used to delete data from a database?**\n\nThe DELETE statement is used to delete existing records in a table.\n\n**DELETE Syntax**\n\n```sql\nDELETE FROM table_name\nWHERE condition;\n```\n\n\n**75. With SQL, how can you delete the records where the \"FirstName\" is \"John\" in the Customers Table?**\n\nDELETE FROM Customers WHERE FirstName = 'John'  \n\n**76. The FROM SQL keyword is used to**\n\nSpecify the table we are are selecting or deleting from.\n\n**DELETE Syntax**\n\n```sql\nDELETE FROM table_name\nWHERE condition;\n```\n\n**UPDATE Syntax**\n\n```sql\nUPDATE table_name\nSET column1 = value1, column2 = value2, ...\nWHERE condition;\n```\n\n**INSERT Syntax**\n\n```sql\nINSERT INTO table_name (column1, column2, column3, ...)\nVALUES (value1, value2, value3, ...);\n```\n\n**SELECT Syntax**\n\n```sql\nSELECT column1, column2, ...\nFROM table_name;\n```\n\n**77. What is the difference between DELETE and TRUNCATE?**\n\nThe basic difference in both is DELETE is DML command and TRUNCATE is DDL.\n\nDELETE is used to delete a specific row from the table whereas TRUNCATE is used to remove all rows from the table\n\nWe can use DELETE with WHERE clause but cannot use TRUNCATE with it.\n\n**78. Which SQL statement is used to create a table in a database?**\n\nThe CREATE TABLE statement is used to create a new table in a database.\n\n**Syntax**\n\n```sql\nCREATE TABLE table_name (\ncolumn1 datatype,\ncolumn2 datatype,\ncolumn3 datatype,\n....\n);\n```\n\n**79. What is Collation in SQL?**\n\nA collation is a set of rules that defines how to compare and sort character strings.\n\n**Detailed Explanation:**\n\nA character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. Let's make the distinction clear with an example of an imaginary character set.\n\nSuppose that we have an alphabet with four letters: A, B, a, b. We give each letter a number: A = 0, B = 1, a = 2, b = 3. The letter A is a symbol, the number 0 is the encoding for A, and the combination of all four letters and their encodings is a character set.\n\nSuppose that we want to compare two string values, A and B. The simplest way to do this is to look at the encodings: 0 for A and 1 for B. Because 0 is less than 1, we say A is less than B. What we've just done is apply a collation to our character set. The collation is a set of rules (only one rule in this case): “compare the encodings.” We call this simplest of all possible collations a binary collation.\n\nBut what if we want to say that the lowercase and uppercase letters are equivalent? Then we would have at least two rules: (1) treat the lowercase letters a and b as equivalent to A and B; (2) then compare the encodings. We call this a case-insensitive collation. It is a little more complex than a binary collation.\n\nIn real life, most character sets have many characters: not just A and B but whole alphabets, sometimes multiple alphabets or eastern writing systems with thousands of characters, along with many special symbols and punctuation marks. Also in real life, most collations have many rules, not just for whether to distinguish lettercase, but also for whether to distinguish accents (an “accent” is a mark attached to a character as in German Ö), and for multiple-character mappings (such as the rule that Ö = OE in one of the two German collations).\n\n_Source: dev.mysql.com_\n\n**80. What is true about an AUTO_INCREMENT column in SQL?**\n\nAUTO_INCREMENT allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.\n\n**Syntax for MySQL**\n\nThe following SQL statement defines the \"ID\" column to be an auto-increment primary key field in the \"Persons\" table:\n\n```sql\nCREATE TABLE Persons (\nID int NOT NULL AUTO_INCREMENT,\nLastName varchar(255) NOT NULL,\nFirstName varchar(255),\nAge int,\nPRIMARY KEY (ID)\n);\n```\n\n**Syntax for SQL Server**\n\nThe following SQL statement defines the \"ID\" column to be an auto-increment primary key field in the \"Persons\" table:\n\n```sql\nCREATE TABLE Persons (\nID int IDENTITY(1,1) PRIMARY KEY,\nLastName varchar(255) NOT NULL,\nFirstName varchar(255),\nAge int\n);\n```\n\n**Syntax for Oracle**\n\nIn Oracle the code is a little bit more tricky.\n\nYou will have to create an auto-increment field with the sequence object (this object generates a number sequence).\n\nUse the following **CREATE SEQUENCE syntax:**\n\n```sql\nCREATE SEQUENCE seq_person\nMINVALUE 1\nSTART WITH 1\nINCREMENT BY 1\nCACHE 10;\n```\n\nThe code above creates a sequence object called seq_person, that starts with 1 and will increment by 1. It will also cache up to 10 values for performance. The cache option specifies how many sequence values will be stored in memory for faster access.\n\nTo insert a new record into the \"Persons\" table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence):\n\n```sql\nINSERT INTO Persons (ID,FirstName,LastName)\nVALUES (seq_person.nextval,'Lars','Monsen');\n```\n\nThe SQL statement above would insert a new record into the \"Persons\" table. The \"ID\" column would be assigned the next number from the seq_person sequence. The \"FirstName\" column would be set to \"Lars\" and the \"LastName\" column would be set to \"Monsen\".\n\n**81. What are valid constraints in MySQL?**\n\nSQL constraints are used to specify rules for the data in a table.\n\nConstraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted.\n\nConstraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.\n\nThe following constraints are commonly used in SQL:\n\n- NOT NULL - Ensures that a column cannot have a NULL value\n\n- UNIQUE - Ensures that all values in a column are different\n\n- PRIMARY KEY - A combination of a NOT NULL and UNIQUE . Uniquely identifies each row in a table\n\n- FOREIGN KEY - Uniquely identifies a row/record in another table\n\n- CHECK - Ensures that all values in a column satisfies a specific condition\n\n- DEFAULT - Sets a default value for a column when no value is specified\n\n- INDEX - Used to create and retrieve data from the database very quickly\n\n  \n\n**82. Which of the following is NOT TRUE about constraints?**\n\nNOT NULL - Ensures that a column cannot have a NULL value\n\nUNIQUE - Ensures that all values in a column are different\n\nPRIMARY KEY - A combination of a NOT NULL and UNIQUE . Uniquely identifies each row in a table\n\nFOREIGN KEY - Uniquely identifies a row/record in another table\n\n  \n\n**83. The NOT NULL constraint enforces a column to not accept null values.**\n\nNOT NULL - Ensures that a column cannot have a NULL value.\n\n**84. An unique (non-key) field**\n\nThe UNIQUE constraint ensures that all values in a column are different.\n\n \n\n**85. What is CHECK Constraint?**\n\nThe CHECK constraint is used to limit the value range that can be placed in a column.\n\nIf you define a CHECK constraint on a single column it allows only certain values for this column.\n\nIf you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.\n\n**SQL CHECK on CREATE TABLE**\n\nThe following SQL creates a CHECK constraint on the \"Age\" column when the \"Persons\" table is created. The CHECK constraint ensures that you can not have any person below 18 years:\n\n**MySQL:**\n\n```sql\nCREATE TABLE Persons (\nID int NOT NULL,\nLastName varchar(255) NOT NULL,\nFirstName varchar(255),\nAge int,\nCHECK (Age\u003e=18)\n);\n```\n\n**SQL Server / Oracle / MS Access:**\n\n-   CREATE TABLE Persons (\n-   ID int NOT NULL,\n-   LastName varchar(255) NOT NULL,\n-   FirstName varchar(255),\n-   Age int CHECK (Age\u003e=18)\n-   );\n\n**86. What is the difference between UNIQUE and PRIMARY KEY constraints?**\n\nUNIQUE vs PRIMARY KEY\n\nBoth the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.\n\nA PRIMARY KEY constraint automatically has a UNIQUE constraint.\n\nHowever, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.\n\n**87. What does SQL DROP TABLE clause do?**\n\nThe DROP TABLE statement is used to drop an existing table in a database.\n\n**Syntax**\n\n```sql\nDROP TABLE table_name;\n```\n\n\n**88. What is the difference between DROP and TRUNCATE?**\n\nTRUNCATE 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.  \n\n**89. How do you add a 'order_date' column to a table called 'order'?**\n\nALTER TABLE order ADD order_date DATE\n\nThe ALTER TABLE statement is used to add, delete, or modify columns in an existing table.\n\nThe ALTER TABLE statement is also used to add and drop various constraints on an existing table.\n\n**ALTER TABLE - ADD Column**\n\nTo add a column in a table, use the following syntax:\n\n```sql\nALTER TABLE table_name\nADD column_name datatype;\n```\n\n**90. What the correct syntax to rename column 'Address' to 'Addr' in 'Customer' table?**\n\nALTER TABLE Customer CHANGE Address Addr varchar(50);\n\nALTER TABLE - CHANGE\n\nYou rename a column using the ALTER TABLE and CHANGE commands together to change an existing column.\n\n**Syntax**\n\n```sql\nALTER TABLE table_name\nCHANGE oldname newname datatype \n```\n\n\n**91. Consider the following schema ADDRESSES (id, street_name, number, city, state) Which code snippet will alter the table ADDRESSES and delete the column named CITY?**\n\nALTER TABLE addresses DROP COLUMN city;\n\nALTER TABLE - DROP COLUMN\n\nTo delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):\n\n```sql\nALTER TABLE table_name\nDROP COLUMN column_name;\n```\n\n**92. What are Indexes in SQL?**\n\nIndexes are used to retrieve data from the database very fast. The users cannot see the indexes, they are just used to speed up searches/queries.\n\nNote: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So, only create indexes on columns that will be frequently searched against.\n\n**CREATE INDEX Syntax**\n\nCreates an index on a table. Duplicate values are allowed:\n\n```sql\nCREATE INDEX index_name\nON table_name (column1, column2, ...)\n```\n\n\n**93. What is the difference between clustered and non-clustered indexes? Which of the following statements are true?**\n\nOne table can have only one clustered index but multiple nonclustered indexes.\n\nClustered indexes can be read rapidly rather than non-clustered indexes.\n\nClustered 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.\n\n**94. The statement for assigning access privileges is**\n\nSQL 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.\n\nGRANT Command: This command is used provide database access to user apart from an administrator.\n\n**Syntax**\n\n```sql\nGRANT privilege_name\nON object_name\nTO {user_name|PUBLIC|role_name}\n[WITH GRANT OPTION];\n```\n\nIn above syntax WITH GRANT OPTIONS indicates that the user can grant the access to another user too.\n\nREVOKE Command: This command is used provide database deny or remove access to database objects.\n\n**Syntax**\n\n```sql\nREVOKE privilege_name\nON object_name\nFROM {user_name|PUBLIC|role_name}\n```\n\n\n**95. How many types of Privileges are available in SQL?**\n\nThere are two types of privileges used in SQL, such as\n\n**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.\n\n**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.\n\n**96. List the various privileges that a user can grant to another user?**\n\nSame answers as for the previous question\n\n**97. What does the term 'locking' refer to?**\n\nLocking is a process preventing users from reading data being changed by other users, and prevents concurrent users from changing the same data at the same time.  \n\n**98. What are a transaction's main controls?**\n\nCOMMIT command is used to permanently save any transaction into the database.\n\nWhen we use any DML command like INSERT , UPDATE or DELETE , the changes made by these commands are not permanent, until the current session is closed, the changes made by these commands can be rolled back.\n\nTo avoid that, we use the COMMIT command to mark the changes as permanent.\n\nFollowing is commit command syntax:\n\n```sql\nCOMMIT;\n```\n\nROLLBACK command\n\nThis command restores the database to last committed state. It is also used with SAVEPOINT command to jump to a savepoint in an ongoing transaction.\n\nIf we have used the UPDATE command to make some changes into the database, and realise that those changes were not required, then we can use the ROLLBACK command to rollback those changes, if they were not committed using the COMMIT command.\n\nFollowing is rollback command syntax:\n\n- _ROLLBACK TO savepoint_name;_\n\n- SAVEPOINT command\n\n- SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point whenever required.  \n\nFollowing is savepoint command syntax:\n\n- _SAVEPOINT savepoint_name;_\n\nIn short, using this command we can name the different states of our data in any table and then rollback to that state using the ROLLBACK command whenever required.\n\n**99. A transaction completes its execution is said to be**\n\nCommitted**.**\n\n**100. Consider the following code: START TRANSACTION /*transaction body*/ COMMIT; ROLLBACK; What does Rollback do?**\n\nIt does nothing. Once a transaction has executed commit, its effects can no longer be undone by rollback.  \n\n**101. What are valid properties of the transaction?**\n\nThe characteristics of these four properties as defined by Reuter and Härder are as follows:\n\n**Atomicity**\n\n​\tAtomicity requires that each transaction be \"all or nothing\": if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible (\"atomic\"), and an aborted transaction does not happen.\n\n**Consistency**\n\n​\tThe consistency property ensures that any transaction will bring the database from one valid state to another. Any data written to the database must be valid according to all defined rules, including constraints, cascades, triggers, and any combination thereof. This does not guarantee correctness of the transaction in all ways the application programmer might have wanted (that is the responsibility of application-level code), but merely that any programming errors cannot result in the violation of any defined rules.\n\n**Isolation**\n\n​\tThe isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict - as opposed to relaxed - serializability), the effects of an incomplete transaction might not even be visible to another transaction.\n\n**Durability**\n\n​\tThe durability property ensures that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory.\n\n \n\n**102. What happens if autocommit is enabled?**\n\nIf autocommit mode is enabled, each SQL statement forms a single transaction on its own. By default, most of DBMS start the session for each new connection with autocommit enabled, so they do a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error.\n\n**103. What is the default isolation level used in MySQL?**\n\n**REPEATABLE READ**\n\n**104. In MySQL autocommit is enabled by default for each session.**\n\n**TRUE**\n\n**105. Which of these is also known as a virtual table in MySQL?**\n\n**A view.**\n\nIn SQL, a view is a virtual table based on the result-set of an SQL statement.\n\nA view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.\n\nYou can add SQL functions, WHERE , and JOIN statements to a view and present the data as if the data were coming from one single table.\n\n**CREATE VIEW Syntax**\n\n```sql\nCREATE VIEW view_name AS\nSELECT column1, column2, ...\nFROM table_name\nWHERE condition;\n```\n\n\n**106. Are views updatable using INSERT, DELETE or UPDATE?**\n\nThe SQL UPDATE VIEW command can be used to modify the data of a view.\n\nAll views are not updatable. So, UPDATE command is not applicable to all views. An updatable view is one which allows performing a UPDATE command on itself without affecting any other table.\n\nWhen can a view be updated?\n\n1. The view is defined based on one and only one table.\n\n2. The view must include the PRIMARY KEY of the table based upon which the view has been created.\n\n3. The view should not have any field made out of aggregate functions.\n\n4. The view must not have any DISTINCT clause in its definition.\n\n5. The view must not have any GROUP BY or HAVING clause in its definition.\n\n6. The view must not have any SUBQUERIES in its definitions.\n\n7. If the view you want to update is based upon another view, the later should be updatable.\n\n8. Any of the selected output fields (of the view) must not use constants, strings or value expressions.  \n\n**107. What are the advantages of Views?**\n\nSimplify queries: You can use database view to hide the complexity of underlying tables to the end-users and external applications\n\nAll changes performed by SQL statements are executed only after a COMMIT command.\n\nA database view helps limit data access to specific users. You may not want a subset of sensitive data can be queryable by all users\n\n**108. Does a View contain data?**\n\nA view does not contain any data of its own, but is like a window through which data from other tables can be viewed and changed.\n\nThe answer depends on the type of view. In case of normal view, the answer is NO it only contains query based on a base table but in case of materialized view, YES it does contain data and for the updated data in the base table, it needs to be refreshed.\n\n**109. What SQL statement do you have to use to remove a view?**\n\nYou can delete a view with the DROP VIEW command.\n\n**SQL DROP VIEW Syntax**\n\n_DROP VIEW view_name;_\n\n**110. Can a View based on another View?**\n\nYes, A View is based on another View.  \n\n**111. Inside a stored procedure you to iterate over a set of rows returned by a query using a**\n\nA CURSOR is a database object which is used to manipulate data in a row-to-row manner.\n\nCursor follows steps as given below:\n\n-   Declare Cursor\n-   Open Cursor\n-   Retrieve row from the Cursor\n-   Process the row\n-   Close Cursor\n-   Deallocate Cursor  \n\n**112. How do you call the process of finding a good strategy for processing a query?**\n\nQuery optimization is a function of many relational database management systems. The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans.\n\n\n**113. What is a trigger? or  There are triggers for…* or  A trigger is applied to**\n\nTriggers in SQL is kind of stored procedures used to create a response to a specific action performed on the table. You can invoke triggers explicitly on the table in the database.\n\nTriggers are, in fact, written to be executed in response to any of the following events:\n\n- A database manipulation (DML) statement (DELETE , INSERT , or UPDATE )\n\n- A database definition (DDL) statement (CREATE , ALTER , or DROP ).\n\n- A database operation (SERVERERROR , LOGON , LOGOFF , STARTUP , or SHUTDOWN ).\n\nAction and Event are two main components of SQL triggers when certain actions are performed the event occurs in response to that action.\n\n**Syntax**\n\n```sql\nCREATE TRIGGER name {BEFORE|AFTER} (event [OR..]}\nON table_name [FOR [EACH] {ROW|STATEMENT}]\nEXECUTE PROCEDURE functionname {arguments}\n```\n\n**114. A special kind of a stored procedure that executes in response to certain action on the table like insertion, deletion or updation of data is called**\n\n**Trigger.**\n\n\n**115. What is SQL Injection?**\n\n- SQL injection is a code injection technique that might destroy your database.\n\n- SQL injection is one of the most common web hacking techniques.\n\n- SQL injection is the placement of malicious code in SQL statements, via web page input.\n\n  \n\n**_Resources_**\n\n[www.wikipedia.org](http://www.wikipedia.org/)\n\n[www.w3schools.com](http://www.w3schools.com/)\n\n[www.freefeast.info](https://freefeast.info/)\n","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoraus%2FCrackingTheSQLInterview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxoraus%2FCrackingTheSQLInterview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxoraus%2FCrackingTheSQLInterview/lists"}