
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL &#8211; CodeInDotNet</title>
	<atom:link href="https://www.codeindotnet.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codeindotnet.com</link>
	<description>C# Dot Net Programming tutorial &#38; code examples</description>
	<lastBuildDate>Tue, 10 Sep 2024 06:27:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>

<image>
	<url>https://www.codeindotnet.com/wp-content/uploads/2021/04/SiteIcon.png</url>
	<title>SQL &#8211; CodeInDotNet</title>
	<link>https://www.codeindotnet.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>70+ Top SQL Interview Questions and Answers</title>
		<link>https://www.codeindotnet.com/sql-interview-questions-answers/</link>
					<comments>https://www.codeindotnet.com/sql-interview-questions-answers/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 19 Jul 2024 15:38:41 +0000</pubDate>
				<category><![CDATA[Interview Questions Answers]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://www.codeindotnet.com/?p=7407</guid>

					<description><![CDATA[SQL Interview Questions Download all Questions as .PDF Search: Clear × What is a database? A database is an organized collection of structured data stored electronically. It allows for efficient storage, retrieval, and management of data. What is SQL? SQL, or Structured Query Language, is a programming language used to manage and interact with databases. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<br><br>



<h2 class="wp-block-heading has-text-align-center h2Cust1"><strong>SQL Interview Questions</strong></h2>



<p class="has-text-align-right"><strong><a href="#download-questions" data-type="internal" data-id="#download-questions">Download all Questions as .PDF</a></strong></p>



<div id="searchPopup">
	<div class="popup-header">			
		<span style="font-weight:bold;">Search: </span>
		<input type="text" id="myInput" autocomplete="off" placeholder=" type keyword(s) &amp; filter Questions..." onclick="document.getElementById('myList').style.display = 'block'; document.getElementById('btnpopclose').style.display = 'block';">
		<button onclick="displayAllResult();" style="padding: 5px 9px; border-radius: 10px; color:#fff;" >Clear</button>
		<button id="btnpopclose" class="popup-close" onclick="document.getElementById('myList').style.display = 'none'; document.getElementById('btnpopclose').style.display = 'none';">×</button>
	</div>
	<div id="myList"></div>		
</div>



<style>
.popup-close {
    font-size: 1.25rem;
    font-weight: 700;
    padding: .3125rem .625rem;
    border-radius: .3125rem;
    background-color: red;
    color: #fff;
    border: none;
    float: right;
    display: none;
    /*margin: -.3125rem;*/
}

#myInput{
/*width: 60%; */
width: calc(100% - 11.5625rem);
display:inline; 
border-radius: 10px; 
margin:0;
}
/*@media screen and (max-width: 750px) {
    #myInput {
        width: 46%;
    }
}*/

#searchPopup{
border: 1px solid #6a409759;
    padding: 10px;
    border-radius: 5px;
}

#myList{
line-height: 1.7; 
font-size: 16px; 
overflow-y: scroll; 
height: 250px; 
/*border-bottom: 3px lightgray solid;*/
display:none; 
margin-top: 15px;
}
</style>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a database?</strong></h3>



<p class="custp1">A <strong>database</strong> is an organized collection of structured data stored electronically. It allows for efficient storage, retrieval, and management of data.</p>



<br>



<h3 class="wp-block-heading hLBRed"><strong>What is SQL?</strong></h3>



<p class="p-mb-0 custp1">SQL, or Structured Query Language, is a programming language used to manage and interact with databases. It allows you to:</p>



<ol class="ol1 ol1-mb-0 wp-block-list">
<li><strong>Retrieve Data</strong>: Get information from a database.</li>



<li><strong>Insert Data</strong>: Add new information to a database.</li>



<li><strong>Update Data</strong>: Change existing information in a database.</li>



<li><strong>Delete Data</strong>: Remove information from a database.</li>
</ol>



<p class="p-mb-0 custp1">In simple terms, SQL helps you work with data stored in a database.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is an SQL statement?</strong></h3>



<p class="custp1">An <strong>SQL statement</strong> ,also known as an SQL command, is used to interact with a database. It can perform various operations such as retrieving , updating , deleting data, or creating database objects like tables and indexes. Some examples of SQL statements are <span class="spanHT">SELECT</span>, <span class="spanHT">CREATE</span>, <span class="spanHT">DELETE</span>, <span class="spanHT">DROP</span>, <span class="spanHT">REVOKE</span>, and so on.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a schema?</strong></h3>



<p class="custp1 p-mb-0">A <strong>schema</strong> is a structure that defines the organization of data in a database. It includes tables, views, indexes, procedures, and other database objects, and helps organize and manage the data logically.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="com"><b>-- To view all schemas</b></span>
<span class="key">SELECT</span> * <span class="key">FROM</span> INFORMATION_SCHEMA.SCHEMATA;

<span class="com"><b>--To view the schema of a specific table</b></span>
<span class="key">SELECT</span> COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
<span class="key">FROM</span> INFORMATION_SCHEMA.COLUMNS
<span class="key">WHERE</span> TABLE_NAME = 'your_table_name'; </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a table and a field in SQL</strong></h3>



<p class="custp1 p-mb-0"><strong>Table</strong>: A table is a collection of related data organized in rows and columns within a database. Each row represents a record, and each column represents a field.</p>



<p><strong>Field</strong>: A field is a single column in a table, representing a specific piece of data within a record. It holds individual data values for each row in the table.</p>



<br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to create a Table?</strong></strong></h3>



<pre class="pchl"><code><span class="key">CREATE</span> <span class="key">TABLE</span> Employees (
    EmployeeID <span style="color:#FF33CC;">INT</span> <span class="key">IDENTITY</span>(1,1) <span class="key">PRIMARY KEY</span>,
    FirstName <span style="color:#FF33CC;">VARCHAR</span>(50) <span class="key">NOT</span> <span class="key">NULL</span>,
    LastName <span style="color:#FF33CC;">VARCHAR</span>(50) <span class="key">NOT</span> <span class="key">NULL</span>,
    BirthDate <span style="color:#FF33CC;">DATE</span>,
    HireDate <span style="color:#FF33CC;">DATE</span>,
    Salary <span style="color:#FF33CC;">DECIMAL</span>(10, 2)
); </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to add a column to a table?</strong></strong></h3>



<p class="custp1 p-mb-0">Use the <span class="spanHT">ALTER TABLE</span> statement with the <span class="spanHT">ADD</span> clause.</p>



<pre class="pchl"><code><span class="key">ALTER TABLE</span> table_name
<span class="key">ADD</span> new_column_name data_type; </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to rename a column of a table?</strong></strong></h3>



<pre class="pchl"><code><span class="key">ALTER</span> <span class="key">TABLE</span> table_name
<span class="key">RENAME</span> <span class="key">COLUMN</span> old_column_name TO new_column_name; </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to delete a column from a table?</strong></strong></h3>



<pre class="pchl"><code><span class="key">ALTER</span> <span class="key">TABLE</span> table_name
<span class="key">DROP</span> <span class="key">COLUMN</span> column_name; </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is an Alias?</strong></h3>



<p class="custp1 p-mb-0">An <strong>alias</strong> is a temporary name given to a table or column for the duration of a query. It helps make the query results more readable and can simplify complex queries.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><b>-- Column Alias</b>
<span class="key">SELECT</span> column_name <span style="background:#f2f19d;"><span class="key"> AS</span> alias_name </span><span class="key">FROM</span> table_name;

<b>-- Table Alias</b>
<span class="key">SELECT</span> t.column1 <span class="key">FROM</span> table_name <span class="key">AS</span> <span style="background:#f2f19d;"> t </span>; </code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Clause?</strong></h3>



<p class="custp1">A <strong>clause</strong> is a part of a SQL statement that specifies a condition or operation. Clauses define how to filter, sort, group, or join data within a query. Some examples are <span class="spanHT">WHERE</span>, <span class="spanHT">LIMIT</span>, <span class="spanHT">HAVING</span>, <span class="spanHT">LIKE</span>, <span class="spanHT">AND</span>, <span class="spanHT">OR</span>, <span class="spanHT">ORDER BY</span>, etc.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is DDL?</strong></h3>



<p class="custp1">Data Definition Language (DDL) is used to define and manage database structures. It includes commands like CREATE, ALTER, and DROP, which are used to create, modify, and delete database objects such as tables, indexes, and schemas.</p>



<br>



<h3 class="wp-block-heading hLBRed"><strong>What is DML?</strong></h3>



<p class="custp1">Data Manipulation Language (DML) is used to manage and manipulate data within database tables. It includes commands like SELECT, INSERT, UPDATE, and DELETE, which are used to retrieve, add, modify, and remove data.</p>



<br>



<h3 class="wp-block-heading hLBRed"><strong>What are the different types of SQL commands?</strong></h3>



<p class="custp1 p-mb-0"><strong>1) Data Definition Language (DDL)</strong>:</p>



<ul class="ol1 wp-block-list">
<li><strong>CREATE</strong>: Creates new database objects like tables, indexes, and views.</li>



<li><strong>ALTER</strong>: Modifies existing database objects.</li>



<li><strong>DROP</strong>: Deletes database objects.</li>



<li><strong>TRUNCATE</strong>: Removes all rows from a table, but the table structure remains.</li>
</ul>



<p class="p-mb-0"><strong>2) Data Manipulation Language (DML)</strong>:</p>



<ul class="ol1 wp-block-list">
<li><strong>SELECT</strong>: Retrieves data from the database.</li>



<li><strong>INSERT</strong>: Adds new rows of data to a table.</li>



<li><strong>UPDATE</strong>: Modifies existing data within a table.</li>



<li><strong>DELETE</strong>: Removes rows of data from a table.</li>
</ul>



<p class="p-mb-0"><strong>3) Data Control Language (DCL)</strong>:</p>



<ul class="ol1 wp-block-list">
<li><strong>GRANT</strong>: Gives users access privileges to the database.</li>



<li><strong>REVOKE</strong>: Removes access privileges from users.</li>
</ul>



<p class="p-mb-0"><strong>4) Transaction Control Language (TCL)</strong>:</p>



<ul class="ol1 wp-block-list">
<li><strong>COMMIT</strong>: Saves all changes made during the current transaction.</li>



<li><strong>ROLLBACK</strong>: Reverts changes made during the current transaction.</li>



<li><strong>SAVEPOINT</strong>: Sets a point within a transaction to which you can later roll back.</li>
</ul>



<p class="p-mb-0"><strong>5) Data Query Language (DQL)</strong>:</p>



<ul class="ol1 wp-block-list">
<li><strong>SELECT</strong>: Used to query and retrieve data from the database.</li>
</ul>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What are the different types of SQL relationships? Can you describe and give examples of each type?</strong></h3>



<br>



<p class="p-mb-0"><strong>1) One-to-One Relationship:</strong> Each row in &#8216;Table A&#8217; is linked to one and only one row in &#8216;Table B&#8217;, and vice versa.</p>



<p class="custp1 p-mb-0"><em><strong>example:</strong></em> A &#8216;Person&#8217; table with a &#8216;Passport&#8217; table where each person has one unique passport.</p>



<pre class="pchl"><code>Person (<span style="background:#f2f19d;">PersonID</span>, Name)
Passport (PassportID, <span style="background:#f2f19d;">PersonID</span>, PassportNumber) </code></pre><br>



<p class="p-mb-0"><strong>2) One-to-Many Relationship:</strong> A row in &#8216;Table A&#8217; can be associated with many rows in &#8216;Table B&#8217;, but a row in &#8216;Table B&#8217; is associated with only one row in &#8216;Table A&#8217;.</p>



<p class="custp1 p-mb-0"><strong><em>example: </em></strong>A &#8216;Customer&#8217; table with an &#8216;Orders&#8217; table where each customer can place multiple orders.</p>



<pre class="pchl"><code>Customer (<span style="background:#f2f19d;">CustomerID</span>, Name)
Orders (OrderID, <span style="background:#f2f19d;">CustomerID</span>, OrderDate) </code></pre><br>



<p class="p-mb-0"><strong>3) Many-to-One Relationship:</strong> (reverse of a one-to-many relationship) Many rows in &#8216;Table A&#8217; can be associated with one row in &#8216;Table B&#8217;.</p>



<p class="custp1 p-mb-0"><strong><em>example:</em></strong> Many employees belonging to one department.</p>



<pre class="pchl"><code>Department (<span style="background:#f2f19d;">DepartmentID</span>, DepartmentName)
Employee (EmployeeID, <span style="background:#f2f19d;">DepartmentID</span>, Name) </code></pre><br>



<p class="p-mb-0"><strong>4) Many-to-Many Relationship:</strong> Rows in &#8216;Table A&#8217; can be associated with many rows in &#8216;Table B&#8217; and vice versa.</p>



<p class="custp1 p-mb-0"><strong><em>example:</em></strong> A &#8216;Students&#8217; table and a &#8216;Courses&#8217; table where each student can enroll in multiple courses, and each course can have multiple students. Use a junction table to establish the relationship.</p>



<pre class="pchl"><code>Students (<span style="background:#f2f19d;">StudentID</span>, StudentName)
Courses (<span style="background:#f2f19d;">CourseID</span>, CourseName)
StudentCourses (<span style="background:#f2f19d;">StudentID, CourseID</span>) </code></pre>



<br><br><br>



<h2 class="wp-block-heading h2Cust1"><strong>Basic SQL Commands<strong> and Queries</strong></strong></h2>



<br>



<h3 class="wp-block-heading hLBRed"><strong>Basic SQL Commands and Queries.</strong></h3>



<pre class="pchl"><code><span class="com"><b>-- Select all columns from a table</b></span>
<span class="key">SELECT</span> <b>*</b> <span class="key">FROM</span> Employees

<span class="com"><b>-- Insert</b></span>
<span class="key">INSERT</span> <span class="key">INTO</span> Employees (EmployeeID, FirstName, LastName, BirthDate, HireDate, Salary)
<span class="key">VALUES</span> (1, 'John', 'Doe', '1980-01-01', '2020-06-15', 50000.00);

<span class="com"><b>-- Update</b></span>
<span class="key">UPDATE</span> Employees
<span class="key">SET</span> Salary = 55000.00
<span class="key">WHERE</span> EmployeeID = 1;

<span class="com"><b>-- Delete</b></span>
<span class="key">DELETE</span> <span class="key">FROM</span> Employees <span class="key">WHERE</span> EmployeeID = 1; 

<span class="com"><b>-- Get the Count of Rows in a Table</b></span>
<span class="key">SELECT</span> <span class="key"><b>COUNT</b></span>(*) <span class="key">FROM</span> Employees;

<span class="com"><b>--Sorting Records in a Table</b></span>
<span class="com">/* ASC specifies ascending order (default).*/</span>
<span class="com">/* DESC specifies descending order. */</span>

<span class="key">SELECT</span> * <span class="key">FROM</span> Employees <span class="key">ORDER BY</span> LastName <span class="key"><b>ASC</b></span>;
<span class="key">SELECT</span> * <span class="key">FROM</span> Employees <span class="key">ORDER BY</span> HireDate <span class="key"><b>DESC</b></span>;

<span class="com"><b>-- Selecting Distinct Records from a Single Table</b></span>
<span class="key">SELECT</span> <span class="key"><b>DISTINCT</b></span> LastName, FirstName <span class="key">FROM</span> Employees;

<span class="com"><b>-- Selecting Distinct Records from Two Tables</b></span>
<span class="key">SELECT</span> EmployeeID, FirstName, LastName <span class="key">FROM</span> Employees
<span class="key"><b>UNION</b></span>
<span class="key">SELECT</span> EmployeeID, FirstName, LastName <span class="key">FROM</span> FormerEmployees;

<span class="com"><b>-- Selecting Common Records from Two Tables</b></span>
<span class="key">SELECT</span> EmployeeID, FirstName, LastName <span class="key">FROM</span> Employees
<span class="key"><b>INTERSECT</b></span>
<span class="key">SELECT</span> EmployeeID, FirstName, LastName <span class="key">FROM</span> FormerEmployees; 
</code></pre>



<br><br><br>



<h2 class="wp-block-heading has-text-align-center h2Cust1" id="Intermediate-SQL-Interview-Questions"><strong>Intermediate SQL Interview Questions and Answers</strong></h2>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Primary Key?</strong></h3>



<p class="custp1">A <strong>primary key</strong> is a column or a set of columns that uniquely identifies each row in a table. It ensures that no duplicate values or NULLs are present in the primary key column(s).</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is Auto Increment?</strong></h3>



<p class="custp1 p-mb-0">Auto Increment is a feature that automatically generates a unique value for a specified column in a table for each new row inserted and is commonly used for primary key columns.</p>



<p class="custp1 p-mb-0"><span class="spanHT">IDENTITY(1,1)</span> is the keyword used to define an auto-increment column. <span class="spanHT">(1,1)</span> specifies the seed and increment values. The first <span class="spanHT">1</span> is the seed (starting value), and the second <span class="spanHT">1</span> is the increment (how much to increase for each new row).</p>



<pre class="pchl"><code><span class="key">CREATE</span> <span class="key">TABLE</span> TableName (
    ID <span class="key">INT</span> <span style="background:#f2f19d;"><span class="key">IDENTITY</span>(1,1)</SPAN> <span class="key">PRIMARY</span> <span class="key">KEY</span>,
); </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a foreign key?</strong></h3>



<p class="custp1">A <strong>foreign key</strong> is a column or a set of columns in one table that links to the primary key in another table. It is used to maintain referential integrity by ensuring that the value in the foreign key column matches a value in the referenced primary key column.</p>



<pre class="pchl"><code><span class="key">CREATE TABLE</span> Employees (
    EmployeeID <span class="key">INT PRIMARY KEY</span>,
    DepartmentID <span class="key">INT</span> <span style="background:#f2f19d;"><span class="key">FOREIGN KEY REFERENCES</span> Departments(DepartmentID)</span>
); </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>Define a Unique Key in SQL</strong></h3>



<p class="custp1">A <strong>unique key</strong> is a column or a set of columns that ensures all values in that column or combination of columns are unique. It prevents duplicate values but allows one NULL value.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a constraint in SQL?</strong></h3>



<p class="custp1 p-mb-0">A <strong>constraint</strong> is a rule applied to a column or a set of columns in a table to enforce data integrity and ensure the accuracy and reliability of the data.</p>



<p class="custp1 p-mb-0"><em><strong><span style="text-decoration: underline;">Common constraints:</span></strong></em></p>



<ol class="ol1 wp-block-list">
<li><strong>Primary Key</strong>: Ensures each row in a table is unique and not NULL.</li>



<li><strong>Foreign Key</strong>: Ensures a column&#8217;s values match values in another table&#8217;s primary key.</li>



<li><strong>Unique</strong>: Ensures all values in a column are unique.</li>



<li><strong>Not Null</strong>: Ensures a column cannot have NULL values.</li>



<li><strong>Check</strong>: Ensures values in a column meet a specific condition.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is Stored Procedure?</strong></h3>



<p class="custp1">A stored procedure is a named set of pre-written SQL commands/statements that is saved in a database in a compiled ready-to-use form and can be executed as a single unit. </p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>Advantages::</u></i></b></div>



<ul class="ol1 wp-block-list">
<li>allows modular programming.</li>



<li>allows faster execution.</li>



<li>reduce the network traffic.</li>



<li>It can be used as a security mechanism.</li>
</ul>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>Types of Stored Procedures in SQL:</u></i></b></div>



<ol class="ol1 wp-block-list">
<li><strong>User-Defined Stored Procedures</strong>: Created by users to perform specific tasks or operations in a database.</li>



<li><strong>System Stored Procedures</strong>: Provided by the database system to perform administrative and system-level tasks.</li>



<li><strong>Temporary Stored Procedures</strong>: Stored in the tempdb database and used for temporary tasks, disappearing after the session ends.</li>



<li><strong>CLR Stored Procedures</strong>: Created using .NET languages and executed within the SQL Server Common Language Runtime (CLR).</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Subquery?</strong></h3>



<p class="custp1 p-mb-0">A <strong>subquery</strong> is a query nested inside another query. It is used to retrieve data that will be used in the main query as a condition to further restrict the data to be retrieved.</p>



<div><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">SELECT</span> Name 
<span class="key">FROM</span> Employees 
<span class="key">WHERE</span> DeptID =<span style="background:#f2f19d;"> (<span class="key">SELECT</span> DeptID <span class="key">FROM</span> Departments <span class="key">WHERE</span> DeptName = 'Sales'); </span></code></pre>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a View?</strong></h3>



<p class="custp1">A view is a virtual table that shows data from base tables and other views (i.e., the outcome of a SELECT query), but not stored separately. It does not copy the memory space.</p>



<p class="p-mb-0">You should use a view in SQL when:</p>



<ol class="wp-block-list">
<li><strong>Simplifying Complex Queries</strong>: To encapsulate complex SQL queries into a single, reusable query.</li>



<li class="custp1"><strong>Enhancing Security</strong>: To restrict user access to specific rows or columns of data.</li>



<li class="custp1"><strong>Ensuring Data Consistency</strong>: To provide a consistent and unified presentation of data, even if the underlying table structure changes.</li>



<li class="custp1"><strong>Providing Data Abstraction</strong>: To present data in a different format or structure without modifying the original tables.</li>



<li class="custp1"><strong>Improving Readability and Maintenance</strong>: To make queries more readable and easier to maintain by breaking down complex logic into simpler, logical units.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Can we create a view based on another view?</strong></h3>



<p class="custp1">Yes. This is also known as nested views.<br><strong><em>note:</em></strong> 1) it can affect performance, 2) the code becomes difficult to read and debug</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Can we still use a view if the original table is deleted?</strong></h3>



<p class="custp1">No, you cannot use a view if the original table is deleted, because a view depends on the original table for its data. If the original table is deleted, the view loses its data source and becomes invalid.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is Trigger?</strong></h3>



<p class="custp1">Triggers are a type of stored procedure that automatically run when data in a table or view is updated, inserted, or deleted. They are powerful tools used to enforce business rules whenever data changes.</p>



<p>A trigger can be executed either &#8216;before or after&#8217; an insert, update, or delete operation.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What are Cursors?</strong></h3>



<p class="custp1">Cursors allow row-by-row processing of the result sets. (or) A cursor in SQL is a database object used to retrieve, manipulate, and navigate through a result set row by row. It acts as a pointer to the rows of a query result.</p>



<p class="p-mb-0"><strong><em>Advantages of Cursors</em></strong></p>



<ol class="wp-block-list">
<li><strong>Row-by-Row Processing</strong>: Allows processing of individual rows, which is useful for complex row-by-row operations.</li>



<li class="custp1"><strong>Simplifies Complex Operations</strong>: Makes it easier to handle complex logic that can&#8217;t be achieved with set-based operations alone.</li>



<li class="custp1"><strong>Enhanced Control</strong>: Provides fine-grained control over query results and how they are processed.</li>
</ol>



<p class="p-mb-0"><strong><em>Disadvantages of Cursors</em></strong></p>



<ol class="wp-block-list">
<li><strong>Performance Overhead</strong>: Can be slower than set-based operations because they process each row individually.</li>



<li class="custp1"><strong>Resource Intensive</strong>: Consume more memory and resources, leading to potential scalability issues.</li>



<li class="custp1"><strong>Complexity</strong>: Can make code more complex and harder to maintain compared to set-based SQL operations.</li>
</ol>



<br>



<h3 class="wp-block-heading hLBRed"><strong>What are indexes? </strong></h3>



<p class="custp1"><strong>Indexes</strong> are special database objects that improve the speed of data retrieval. They act like pointers to quickly locate data within a table.</p>



<p class="p-mb-0"><strong><em>Types of Indexes:</em></strong></p>



<ol class="wp-block-list">
<li><strong>Clustered Index</strong>: Organizes the actual data rows in the table based on the index key. Only one per table.</li>



<li class="custp1"><strong>Nonclustered Index</strong>: Creates a separate structure pointing to the data rows. Multiple NonClustered indexes can exist per table.</li>



<li class="custp1"><strong>Unique Index</strong>: Ensures that all the values in the index key column are unique. It can be Clustered or NonClustered.</li>



<li class="custp1"><strong>Full-Text Index</strong>: Used for efficient searching of large text-based data. Supports full-text queries.</li>



<li class="custp1"><strong>Spatial Index</strong>: Used for indexing spatial data types like geometry or geography. Optimizes spatial queries.</li>



<li class="custp1"><strong>Filtered Index</strong>: NonClustered index with a WHERE clause. Indexes a subset of rows in a table.</li>



<li class="custp1"><strong>XML Index</strong>: Used for indexing XML data type columns. Optimizes XML data queries.</li>
</ol>



<p>Clustered and Non-clustered indexes are often used in real-time project.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is GROUP BY in SQL?</strong></h3>



<p class="custp1"><strong>GROUP BY</strong> is a clause used to group rows that have the same values in specified columns. It allows you to perform aggregate functions (like COUNT, SUM, AVG) on each group of rows.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the use of ORDER BY</strong>?</h3>



<p class="custp1 p-mb-0"><strong>ORDER BY</strong> is a clause used to sort the result set of a query by one or more columns, either in ascending (default) or descending order.</p>



<div><b><i><u>example:</u></i></b></div>



<p class="custp1"><span class="spanHT">SELECT * FROM products ORDER BY price DESC;</span></p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Difference between Primary key and Unique key?</strong></h3>



<br>



<ol class="wp-block-list">
<li><strong>Uniqueness</strong>: Both primary key and unique key ensure all values in the column are unique.</li>



<li class="custp1"><strong>Null Values</strong>: Primary key does not allow NULL values, while unique key allows one NULL value.</li>



<li class="custp1"><strong>Number of Keys Allowed</strong>: A table can have only one primary key but can have multiple unique keys.</li>



<li class="custp1"><strong>Index Creation</strong>: Primary key creates a clustered index by default (if supported), whereas unique key creates a non-clustered index by default.</li>



<li class="custp1"><strong>Purpose</strong>: Primary key uniquely identifies each record in a table, while unique key ensures the uniqueness of values in specific columns.</li>



<li class="custp1"><strong>Identification</strong>: Primary key is used to uniquely identify a row, while unique key is used to enforce uniqueness in columns.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Define Candidate key, Alternate key and Composite key.</strong></h3>



<p class="custp1"><strong>Candidate Key</strong>: A candidate key is a column, or a set of columns, that can uniquely identify any record in a table. A table can have multiple candidate keys, but only one is chosen as the primary key.</p>



<p><strong>Alternate Key</strong>: An alternate key is any candidate key that is not chosen as the primary key. It is still capable of uniquely identifying records but serves as an alternative to the primary key.</p>



<p><strong>Composite Key</strong>: A composite key is a key that consists of two or more columns used together to uniquely identify a record. This is used when a single column is not sufficient to ensure uniqueness.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Different between Delete and Truncate?</strong></h3>



<br>



<ol class="custp1 wp-block-list">
<li><strong>Operation Type</strong>: DELETE is a DML (Data Manipulation Language) command, while TRUNCATE is a DDL (Data Definition Language) command.</li>



<li class="custp1"><strong>Row-by-Row Deletion</strong>: DELETE can remove specific rows based on conditions, but TRUNCATE removes all rows from a table at once.</li>



<li class="custp1"><strong>WHERE Clause</strong>: DELETE allows the use of a WHERE clause (criteria) to filter rows, but TRUNCATE does not.</li>



<li class="custp1"><strong>Transaction Logging</strong>: DELETE logs each row deletion, making it slower, whereas TRUNCATE logs less (deallocation of data page), making it faster.</li>



<li class="custp1"><strong>Space Reclaiming</strong>: DELETE does not immediately free up space, but TRUNCATE does.</li>



<li class="custp1"><strong>Triggers Activation</strong>: DELETE activates triggers, but TRUNCATE does not.</li>



<li class="custp1"><strong>Foreign Key Constraints</strong>: DELETE can be used with foreign key constraints, but TRUNCATE cannot if foreign keys are present.</li>



<li class="custp1"><strong>Identity Reset</strong>: DELETE does not reset the identity column, while TRUNCATE does.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the On Delete cascade constraint?</strong></h3>



<br>



<p class="p-mb-0">The <span class="spanHT">ON DELETE CASCADE</span> constraint in SQL automatically deletes rows in a child table when the corresponding rows in the parent table are deleted.</p>



<pre class="pchl"><code><span class="com"><b>/* -- Child Table -- */</b></span>
<span class="key">CREATE TABLE</span> Employees (
    EmployeeID <span class="key">INT PRIMARY KEY</span>,
    DepartmentID <span class="key">INT</span>,
    <span class="key">FOREIGN KEY</span> (DepartmentID) <span class="key">REFERENCES</span> Departments(DepartmentID) <span class="key" style="background:#f2f19d;">ON DELETE CASCADE</span>
); </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is join in SQL</strong>? <strong>What are the different types of joins? What is the difference between them?</strong></h3>



<p class="custp1"><strong>Joins</strong> are used to combine rows from two or more tables based on a related column between them. There are different types of joins &#8211; Inner Join, Left Join, Right Join, Full Join, Cross Join and Sefl Join.</p>



<p class="p-mb-0"><strong><em>Types of Joins and differences:</em></strong></p>



<ol class="wp-block-list">
<li><strong>Inner Join &#8211;</strong> Returns rows with matching values in both tables. <br><span class="spanHT">SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id</span></li>



<li class="custp1"><strong>Left Join (or Left Outer Join) &#8211;</strong> Returns all rows from the left table and matching rows from the right table. If no match, returns NULLs for the right table. <br><span class="spanHT">SELECT * FROM table1 <b>LEFT JOIN</b> table2 ON table1.id = table2.id</span></li>



<li class="custp1"><strong>Right Join (or Right Outer Join) &#8211; </strong>Returns all rows from the right table and matching rows from the left table. If no match, returns NULLs for the left table. <br><span class="spanHT">SELECT * FROM table1 <b>RIGHT JOIN</b> table2 ON table1.id = table2.id</span></li>



<li class="custp1"><strong>Full Join (or Full Outer Join) &#8211;</strong> Returns rows when there is a match in one of the tables. If no match, returns NULLs for non-matching rows from both tables. <br><span class="spanHT">SELECT * FROM table1 <b>FULL JOIN</b> table2 ON table1.id = table2.id</span></li>



<li class="custp1"><strong>Cross Join &#8211;</strong> Returns the Cartesian product of both tables, meaning all possible combinations of rows <br><span class="spanHT">SELECT * FROM table1 <b>CROSS JOIN</b> table2</span></li>



<li class="custp1"><strong>Self Join &#8211;</strong> Joins a table to itself <br><span class="spanHT">SELECT a.<em>, b.</em> FROM table a, table b WHERE a.id = b.ref_id</span></li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What are Clustered and Non-clustered Indexes?</strong></h3>



<p class="custp1 p-mb-0"><strong>Clustered Index:</strong> A clustered index organizes the actual data rows in a table based on the index key. It&#8217;s like sorting a book by its chapters; there&#8217;s only one way to arrange the pages. A table can have only one clustered index.</p>



<p class="custp1"><strong>Nonclustered Index:</strong> A nonclustered index creates a separate structure that points to the data rows, like an index at the back of a book pointing to pages. It does not change the order of the actual data. A table can have multiple nonclustered indexes.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the difference between Clustered and Non-clustered indexes?</strong></h3>



<br>



<table>
    <thead>
        <tr>
            <th></th>
            <th>Clustered Index</th>
            <th>Nonclustered Index</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th><b>Storage</b></th>
            <td>Stores the actual data rows in the order of the index key</td>
            <td>Stores a separate structure pointing to the data rows</td>
        </tr>
        <tr>
            <th><b>Table Limit</b></th>
            <td>Only one clustered index per table</td>
            <td>Multiple nonclustered indexes per table</td>
        </tr>
        <tr>
            <th><b>Data Access Speed</b></th>
            <td>Generally faster for retrievals that use the index</td>
            <td>Slightly slower than clustered indexes for data retrieval</td>
        </tr>
        <tr>
            <th><b>Physical Order</b></th>
            <td>Reorders the physical data in the table</td>
            <td>Does not affect the physical order of data</td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?</strong></h3>



<p class="custp1">You can use Having Clause with the GROUP BY function in a query and WHERE Clause is applied to each row before, they are part of the GROUP BY function in a query.</p>



<table>
    <thead>
        <tr>
            <th>WHERE Clause</th>
            <th>HAVING Clause</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Filters rows before grouping</td>
            <td>Filters groups after grouping</td>
        </tr>
        <tr>
            <td>Cannot be used with aggregate functions</td>
            <td>Can be used with aggregate functions</td>
        </tr>
        <tr>
            <td>Applied to individual rows</td>
            <td>Applied to groups of rows</td>
        </tr>
        <tr>
            <td>SELECT * FROM table WHERE condition;</td>
            <td>SELECT column, COUNT(*) FROM table GROUP BY column HAVING condition;</td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the difference between BETWEEN and IN operators?</strong></h3>



<br>



<p class="p-mb-0"><strong>1) BETWEEN operator:</strong> To filter the results within a specified range.</p>



<pre class="pchl"><code><span class="key">SELECT</span> * <span class="key">FROM</span> Employees
<span class="key">WHERE</span> Salary <span style="background:#f2f19d;"><span class="key">BETWEEN</span> 30000 <span class="key">AND</span> 50000</span>; </code></pre>



<p class="custp1 p-mb-0"><strong>2) IN operator:</strong> To filter the results based on a specified set of values.</p>



<pre class="pchl"><code><span class="key">SELECT</span> * <span class="key">FROM</span> Employees
<span class="key">WHERE</span> DepartmentID <span style="background:#f2f19d;"><span class="key">IN</span> (1, 4, 8)</span>; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the difference between UNION and UNION ALL SQL syntax?</strong></h3>



<p class="custp1">UNION syntax is used to select information from two or more tables. But it selects only distinct records from tables, while UNION ALL selects all records from tables.<br><strong><em>Note:</em> </strong>Selected records should have the same datatype else the syntax will not work.</p>



<table>
    <thead>
        <tr>
            <th>UNION</th>
            <th>UNION ALL</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Removes duplicate rows</td>
            <td>Includes all duplicate rows</td>
        </tr>
        <tr>
            <td>Slower due to duplicate removal</td>
            <td>Faster as it does not remove duplicates</td>
        </tr>
        <tr>
            <td>Use when you need a distinct set of rows</td>
            <td>Use when you need all rows, including duplicates</td>
        </tr>
        <tr>
            <td>SELECT * FROM table1 <br><b>UNION</b><br> SELECT * FROM table2;</td>
            <td>SELECT * FROM table1 <br><b>UNION ALL</b><br> SELECT * FROM table2;</td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>Different between UNION and Joins</strong></h3>



<br>



<table>
    <thead>
        <tr>
            <th>UNION</th>
            <th>Joins</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Combines results from two or more queries</td>
            <td>Combines columns from two or more tables based on a related column</td>
        </tr>
        <tr>
            <td>Stacks results vertically (adds rows)</td>
            <td>Merges results horizontally (adds columns)</td>
        </tr>
        <tr>
            <td>Removes duplicates (UNION) or includes all (UNION ALL)</td>
            <td>Keeps all matching rows</td>
        </tr>
        <tr>
            <td>Used when you have similar data in different tables that you want to combine</td>
            <td>Used when you need to retrieve related data from multiple tables</td>
        </tr>
        <tr>
            <td>SELECT column1 FROM table1 <br><b>UNION</b><br> SELECT column1 FROM table2;</td>
            <td>SELECT table1.column1, table2.column2 FROM table1 <br><b>JOIN</b><br> table2 ON table1.id = table2.id;</td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What do you mean by a NULL value in SQL?</strong></h3>



<p class="custp1">A <strong>NULL value</strong> represents missing or unknown data. It is different from zero, an empty string, or any other value. NULL indicates the absence of data in a column that the value for a particular column is not known or does not exist.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the difference between COALESCE() &amp; ISNULL()?</strong></h3>



<br>



<table>
    <thead>
        <tr>
            <th>COALESCE()</th>
            <th>ISNULL()</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Returns the first non-null value from a list</td>
            <td>Replaces null with a specified value</td>
        </tr>
        <tr>
            <td>COALESCE(expression1, expression2, &#8230;, expressionN)</td>
            <td>ISNULL(expression, replacement_value)</td>
        </tr>
        <tr>
            <td>Multiple arguments</td>
            <td>Two arguments only</td>
        </tr>
        <tr>
            <td>Standard SQL</td>
            <td>Specific to SQL Server</td>
        </tr>
        <tr>
            <td>Useful when checking multiple columns or expressions</td>
            <td>Simple null replacement with one value</td>
        </tr>
        <tr>
            <td><pre class="pchl"><code><span class="key">SELECT</span> <span style="color:#FF33CC;"><b>COALESCE</b></span>(FirstName, LastName, &#39;NoName&#39;) <span class="key">AS</span> Name</code></pre></td>
            <td><pre class="pchl"><code><span class="key">SELECT</span> <span style="color:#FF33CC;"><b>ISNULL</b></span>(FirstName, &#39;NoName&#39;) <span class="key">AS</span> Name</code></pre></td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What are the different types of SQL operators?</strong></h3>



<br>



<ul class="ol1 wp-block-list">
<li>Arithmetic (<span class="spanHT">+</span>, <span class="spanHT">&#8211;</span>, <span class="spanHT">*</span>, <span class="spanHT">/</span>, etc.)</li>



<li>Comparison (<span class="spanHT">&gt;</span>, <span class="spanHT">&lt;</span>, <span class="spanHT">=</span>, <span class="spanHT">&gt;=</span>, etc.)</li>



<li>Compound (<span class="spanHT">+=</span>, <span class="spanHT">-=</span>, <span class="spanHT">*=</span>, <span class="spanHT">/=</span>, etc.)</li>



<li>Logical (<span class="spanHT">AND</span>, <span class="spanHT">OR</span>, <span class="spanHT">NOT</span>, <span class="spanHT">BETWEEN</span>, etc.)</li>



<li>String (<span class="spanHT">%</span>, <span class="spanHT">_</span>, <span class="spanHT">+</span>, <span class="spanHT">^</span>, etc.)</li>



<li>Set (<span class="spanHT">UNION</span>, <span class="spanHT">UNION ALL</span>, <span class="spanHT">INTERSECT</span>, and <span class="spanHT">MINUS</span> (or <span class="spanHT">EXCEPT</span>))</li>
</ul>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>What operator is used in the query for pattern (word/text) matching?</strong></strong></h3>



<p class="custp1">The&nbsp;<span class="spanHT">LIKE</span>&nbsp;operator in combination with the&nbsp;<span class="spanHT">%</span>&nbsp;and&nbsp;<span class="spanHT">_</span>&nbsp;wildcards. The&nbsp;<span class="spanHT">%</span>&nbsp;wildcard represents any number of characters including zero, while&nbsp;<span class="spanHT">_</span>&nbsp;– strictly one character.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is the correct order of statements in a SELECT query?</strong></h3>



<br>



<p><span class="spanHT">SELECT</span>&nbsp;–&gt;&nbsp;<span class="spanHT">FROM</span>&nbsp;–&gt;&nbsp;<span class="spanHT">JOIN</span>&nbsp;–&gt;&nbsp;<span class="spanHT">ON</span>&nbsp;–&gt;&nbsp;<span class="spanHT">WHERE</span>&nbsp;–&gt;&nbsp;<span class="spanHT">GROUP BY</span>&nbsp;–&gt;&nbsp;<span class="spanHT">HAVING</span>&nbsp;–&gt;&nbsp;<span class="spanHT">ORDER BY</span>&nbsp;–&gt;&nbsp;<span class="spanHT">LIMIT</span></p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>In what order does the interpreter execute the statements in a SELECT query?</strong></h3>



<br>



<p><span class="spanHT">FROM</span>&nbsp;–&gt;&nbsp;<span class="spanHT">JOIN</span>&nbsp;–&gt;&nbsp;<span class="spanHT">ON</span>&nbsp;–&gt;&nbsp;<span class="spanHT">WHERE</span>&nbsp;–&gt;&nbsp;<span class="spanHT">GROUP BY</span>&nbsp;–&gt;&nbsp;<span class="spanHT">HAVING</span>&nbsp;–&gt;&nbsp;<span class="spanHT">SELECT</span>&nbsp;–&gt;&nbsp;<span class="spanHT">ORDER BY</span>&nbsp;–&gt;&nbsp;<span class="spanHT">LIMIT</span></p>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Function in SQL.</strong></h3>



<p class="custp1 p-mb-0">A <strong>function</strong> is a reusable set of SQL statements that perform a specific task and return a single value. Functions can take input parameters and are often used to encapsulate logic that can be applied to data within queries.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><u>Examples of Functions</u></b></div>



<p class="custp1 p-mb-0"><strong>1) Built-in Functions:</strong> SUM(), AVG(), COUNT(), MAX(), MIN()</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">SELECT</span> <span class="key"><b>SUM</b></span>(Amount) <span class="key">AS</span> TotalSales <span class="key">FROM</span> Sales; </code></pre><br>



<p class="custp1 p-mb-0"><strong>2) User-defined Functions:</strong> Functions created by users to perform custom operations.</p>



<p class="custp1 p-mb-0"><strong><em><span style="text-decoration: underline;">Syntax:</span></em></strong></p>



<pre class="pchl"><code><span class="key">CREATE</span> <span class="key">FUNCTION</span> dbo.Square(@Number <span class="key">INT</span>)
<span class="key">RETURNS INT</span>
<span class="key">AS</span>
<span class="key">BEGIN</span>
    <span class="key">RETURN</span> @Number * @Number;
<span class="key">END</span>; </code></pre>



<p class="custp1 p-mb-0"><strong><em><span style="text-decoration: underline;">Usage:</span></em></strong></p>



<pre class="pchl"><code><span class="key">SELECT</span> <b>dbo.Square(4)</b> <span class="key">AS</span> SquareOfFour;  <span class="com">-- Returns 16</span> </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is Pre-defined function in SQL?</strong></h3>



<p class="custp1 p-mb-0">A <strong>pre-defined function</strong> in SQL is a built-in function provided by the database system that performs a specific operation. These functions are ready to use and do not require user-defined code.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>pre-defined functions:</u></i></b></div>



<ul class="ol1 wp-block-list">
<li><strong>Aggregate Functions</strong>: SUM(), AVG(), COUNT()</li>



<li><strong>String Functions</strong>: CONCAT(), UPPER(), LOWER()</li>



<li><strong>Date Functions</strong>: NOW(), DATEADD(), DATEDIFF()</li>



<li><strong>Numeric Functions</strong>: ROUND(), ABS(), SQRT()</li>
</ul>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What are Aggregate functions? name a few.</strong></h3>



<p class="custp1 p-mb-0"><strong>Aggregate functions</strong> in SQL perform calculations on multiple rows of a table and return a single value.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>aggregate functions:</u></i></b></div>



<ol class="ol1 custp1 wp-block-list">
<li><strong>COUNT</strong>: Returns the number of rows.</li>



<li><strong>SUM</strong>: Returns the total sum of a numeric column.</li>



<li><strong>AVG</strong>: Returns the average value of a numeric column.</li>



<li><strong>MAX</strong>: Returns the highest value in a column.</li>



<li><strong>MIN</strong>: Returns the lowest value in a column.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is Window Function in SQL?</strong></h3>



<p class="custp1 p-mb-0">A <strong>window function</strong> performs calculations across a set of table rows related to the current row. Unlike aggregate functions, it doesn&#8217;t group rows into a single output but provides a result for each row, maintaining the individual row identities.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>window functions:</u></i></b></div>



<ul class="ol1 wp-block-list">
<li><strong>ROW_NUMBER()</strong>: Assigns a unique number to each row within a partition.</li>



<li><strong>RANK()</strong>: Assigns a rank to each row within a partition, allowing for ties.</li>



<li><strong>SUM()</strong>: Calculates a running total for a column within a partition.</li>
</ul>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What Scalar Functions</strong>? <strong>Can you name a few examples?</strong></h3>



<p class="custp1 p-mb-0">Scalar function operate on a single value and return a single value. It can be used in the <span class="spanHT">SELECT</span> statement, <span class="spanHT">WHERE</span> clause, or anywhere an expression is allowed. </p>



<p class="custp1">Some example are &#8211; <span class="spanHT">LENGTH()</span>, <span class="spanHT">UPPER()</span>, <span class="spanHT">LOWER()</span>, <span class="spanHT">INITCAP()</span>, <span class="spanHT">SUBSTR()</span>, <span class="spanHT">ROUND()</span>, <span class="spanHT">NOW()</span>, <span class="spanHT">CAST()</span>, <span class="spanHT">CONVERT()</span>.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>What is the&nbsp;CASE()&nbsp;function?</strong></strong></h3>



<p class="custp1">The CASE function allows you to perform conditional logic in your queries. It returns different values based on specified conditions.</p>



<pre class="pchl"><code><span class="key">SELECT</span> 
    <span class="key">CASE</span>
        <span class="key">WHEN</span> Salary &gt 50000 <span class="key">THEN</span> 'High'
        <span class="key">WHEN</span> Salary &gt 30000 <span class="key">THEN</span> 'Medium'
        <span class="key">ELSE</span> 'Low'
    <span class="key">END AS</span> SalaryLevel
<span class="key">FROM</span> Employees; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a temporary table in SQL, and what are the different types of temporary tables?</strong></h3>



<p class="custp1 p-mb-0">A temporary table is a short-lived table used to store data temporarily during a session or for intermediate results in a query.</p>



<p class="custp1 p-mb-0"><strong><span style="text-decoration: underline;">Two types of Temporary Tables:</span></strong></p>



<ul class="ol1 wp-block-list">
<li><strong>Local Temporary Table</strong>: Prefixed with <span class="spanHT">#</span>, limited to the current session.</li>



<li><strong>Global Temporary Table</strong>: Prefixed with <span class="spanHT">##</span>, available to all sessions until the last session ends.</li>
</ul>



<p class="custp1 p-mb-0"><strong><span style="text-decoration: underline;">Usage:</span></strong></p>



<p class="custp1 p-mb-0"><strong><em>Method 1:</em></strong> To insert data into a temporary table <em>without explicitly creating the table structure</em>, you can use the <span class="spanHT">SELECT</span> <span class="spanHT">INTO</span> statement. This statement creates the temporary table and inserts the data in a single step.</p>



<pre class="pchl"><code><span class="key">SELECT</span> EmployeeID, FirstName, LastName
<span style="background:#f2f19d;"><span class="key">INTO</span> #TempTable</span>
<span class="key">FROM</span> Employees
<span class="key">WHERE</span> DepartmentID = 1; </code></pre>
<br>



<p class="custp1 p-mb-0"><strong><em>Method 2:</em></strong> Create a temporary table and then insert data implicitly.</p>



<pre class="pchl"><code><span class="key">CREATE TABLE</span> <span style="background:#f2f19d;">#TempTable</span> (
    Column1 <span class="key">INT</span>,
    Column2 NVARCHAR(50)
); 

<span class="com">-- Verify the data in the temporary table</span>
<span class="key">SELECT</span> * <span class="key">FROM</span> #TempEmployees;</code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Table Variable?</strong></h3>



<p class="custp1 p-mb-0">A table variable is a type of variable used to store temporary data in a structured format, similar to a table, within the scope of a batch, stored procedure, or function. <span class="spanHT">@</span> is used for declaring Table variables. </p>



<pre class="pchl"><code><span class="com">/* Creating a Table Variable */</span>
<span class="key">DECLARE</span> <span style="background:#f2f19d;">@TempTable</span> <span class="key">TABLE</span> (
    Column1 <span class="key">INT</span>,
    Column2 <span class="key">NVARCHAR</span>(50)
);

<span class="com">/* Insert data into the table variable */</span>
<span class="com">-- Method 1: Implicit Insertion</span>
<span style="background:#f2f19d;"><span class="key">INSERT INTO</span> @TempTable</span> (Column1, Column2)
<span class="key">VALUES</span> (1, 'Sample'), (2, 'Example');

<span class="com">-- Method 2: Explicit Insertion</span> 
<span style="background:#f2f19d;"><span class="key">INSERT INTO</span> @TempTable</span>(Column1, Column2)
<span class="key" style="background:#f2f19d;">SELECT</span> Column1, Column2 <span class="key">FROM</span> ExistingTable <span class="key">WHERE</span> SomeCondition;


<span class="com">/* Select data from the table variable */</span>
<span class="key">SELECT</span> * <span class="key">FROM</span> @TempTable; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>Difference Between Temporary Table and Table Variable.</strong></h3>



<br>
<table>
    <thead>
        <tr>
            <th></th>
            <th>Temporary Table</th>
            <th>Table Variable</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th><b>Creation Syntax</b></th>
            <td>CREATE TABLE #TempTable</td>
            <td>DECLARE @TableVariable TABLE</td>
        </tr>
        <tr>
            <th><b>Scope</b></th>
            <td>Session or connection</td>
            <td>Batch, stored procedure, or function</td>
        </tr>
        <tr>
            <th><b>Prefix Used</b></th>
            <td># for local, ## for global</td>
            <td>@</td>
        </tr>
        <tr>
            <th><b>Transaction Support</b></th>
            <td>Fully supported</td>
            <td>Limited</td>
        </tr>
        <tr>
            <th><b>Performance</b></th>
            <td>Better for larger datasets</td>
            <td>Better for smaller datasets</td>
        </tr>
        <tr>
            <th><b>Index Creation</b></th>
            <td>Can create indexes</td>
            <td>Cannot create indexes explicitly</td>
        </tr>
        <tr>
            <th><b>Storage Location</b></th>
            <td>Uses TempDB</td>
            <td>Uses memory</td>
        </tr>
        <tr>
            <th><b>Lifetime</b></th>
            <td>Exists until session ends</td>
            <td>Exists until batch or procedure ends</td>
        </tr>
    </tbody>
</table>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is CTE</strong> (<strong>Common Table Expression</strong>)?</h3>



<p class="custp1">A <strong>CTE (Common Table Expression)</strong> is a temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. It makes complex queries easier to read and manage by breaking them into simpler parts. CTEs are defined using the <span class="spanHT">WITH</span> keyword.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">WITH</span> SalesCTE <span class="key">AS</span> (
    <span class="com">-- CTE query</span>
    <span class="key">SELECT</span> SalesPerson, <span class="key">SUM</span>(SalesAmount) <span class="key">AS</span> TotalSales
    <span class="key">FROM</span> Sales
    <span class="key">GROUP</span> <span class="key">BY</span> SalesPerson
)
<span class="com">-- Main query using the CTE</span>
<span class="key">SELECT</span> SalesPerson, TotalSales
<span class="key">FROM</span> SalesCTE
<span class="key">WHERE</span> TotalSales &gt 10000; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is MERGE in SQL? Give a basic example.</strong></h3>



<p class="custp1 p-mb-0"><strong>MERGE</strong> is a statement that allows you to perform INSERT, UPDATE, and DELETE operations in a single statement. It is used to synchronize two tables by merging them based on a specified condition.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">MERGE</span> <span class="key">INTO</span> Employees <span class="key">AS</span> target
<span class="key">USING</span> NewEmployees <span class="key">AS</span> source
<span class="key">ON</span> target.EmployeeID = source.EmployeeID
<span class="key">WHEN</span> <span class="key">MATCHED</span> <span class="key">THEN</span>
    <span class="key">UPDATE</span> <span class="key">SET</span> target.Name = source.Name, target.Salary = source.Salary
<span class="key">WHEN</span> <span class="key">NOT</span> <span class="key">MATCHED</span> <span class="key">BY</span> <span class="key">TARGET</span> <span class="key">THEN</span>
    <span class="key">INSERT</span> (EmployeeID, Name, Salary) <span class="key">VALUES</span> (source.EmployeeID, source.Name, source.Salary)
<span class="key">WHEN</span> <span class="key">NOT</span> <span class="key">MATCHED</span> <span class="key">BY</span> <span class="key">SOURCE</span> <span class="key">THEN</span>
    <span class="key">DELETE</span>; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is normalization? What are the different types of normalization?</strong></h3>



<p class="custp1"><strong>Normalization</strong> is the process of organizing a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller, related tables and defining relationships between them. Normalization helps maintain data accuracy and efficiency in a database.</p>



<p class="p-mb-0"><strong><em>Types of Normalization:</em></strong></p>



<ol class="wp-block-list">
<li><strong>First Normal Form (1NF)</strong>: Ensures each column contains only atomic (indivisible) values and each entry in a column is of the same type.</li>



<li class="custp1"><strong>Second Normal Form (2NF)</strong>: Meets all the requirements of 1NF and ensures that all non-key columns are fully dependent on the primary key.</li>



<li class="custp1"><strong>Third Normal Form (3NF)</strong>: Meets all the requirements of 2NF and ensures that all columns are directly dependent on the primary key, eliminating transitive dependencies.</li>



<li class="custp1"><strong>Boyce-Codd Normal Form (BCNF)</strong>: A stricter version of 3NF where every determinant is a candidate key.</li>



<li class="custp1"><strong>Fourth Normal Form (4NF)</strong>: Meets all the requirements of BCNF and ensures that multi-valued dependencies are eliminated.</li>



<li class="custp1"><strong>Fifth Normal Form (5NF)</strong>: Ensures that any remaining anomalies are eliminated, usually dealing with join dependencies.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is denormalization?</strong></h3>



<p class="custp1"><strong>Denormalization</strong> is the process of combining tables to reduce the number of joins needed for queries. This can improve read performance by storing redundant data, making data retrieval faster and simpler. However, it may lead to increased storage requirements and potential data inconsistencies.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>How can you raise custom errors from stored procedure?</strong></h3>



<br>



<p>The <span class="spanHT">RAISERROR</span> statement is used to raise custom errors from a stored procedure.</p>



<div><b><i><u>syntax:</u></i></b></div>



<pre class="pchl"><code>RAISERROR ('error message', 10, 1);</code></pre>



<p class="custp1 p-mb-0">It accepts three parameters:</p>



<ol class="ol1 wp-block-list">
<li><strong>Error Message:</strong> Custom text message </li>



<li><strong>Error Severity:</strong> Severity levels from 11 to 19 can be used for user-defined errors</li>



<li><strong>Error State:</strong> State is an integer from 0 to 255</li>
</ol>



<div><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">ALTER</span> <span class="key">PROCEDURE</span> AddCustomer
	@Column2 <span class="key">int</span> =NULL
<span class="key">AS</span>

<span class="key">DECLARE</span> @ErrorMsgID <span class="key">int</span>

<span class="key">INSERT</span> Customer <span class="key">VALUES</span> (@Column2)
	
<span class="key">SET</span> @ErrorMsgID = <span style="background:#f2f19d;"><b> @@ERROR </b></span>
	
<span class="key">IF</span> @ErrorMsgID &lt;&gt 0
<span class="key">BEGIN</span>
	<span style="background:#f2f19d;"><span class="key"><b> RAISERROR</b></span> (<span class="com">'An error occured updating the Customer table'</span>, 10, 1) </span>
<span class="key">END</span> </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Transaction in SQL?</strong></h3>



<p class="custp1 p-mb-0">A <strong>transaction</strong> is a sequence of one or more SQL operations treated as a single unit of work. All operations within a transaction must be completed successfully; otherwise, none of them are applied to the database.</p>



<p class="custp1 p-mb-0"><strong>Purpose &#8211;</strong> ensure data integrity and consistency. It follows the ACID properties (Atomicity, Consistency, Isolation, Durability)</p>



<p class="custp1 p-mb-0"><strong>When to Use &#8211; </strong>Use transactions when you need to ensure that a series of operations are completed successfully together. Common scenarios include:</p>



<ul class="ol1 wp-block-list">
<li>Transferring funds between accounts.</li>



<li>Inserting related data into multiple tables.</li>



<li>Updating multiple records in a dependent manner.</li>
</ul>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">BEGIN</span> TRANSACTION;

<span class="key">UPDATE</span> Accounts
<span class="key">SET</span> Balance = Balance - 100
<span class="key">WHERE</span> AccountID = 1;

<span class="key">UPDATE</span> Accounts
<span class="key">SET</span> Balance = Balance + 100
<span class="key">WHERE</span> AccountID = 2;

<span class="key">IF</span> <span style="color:#FF33CC;">@@ERROR</span> = 0
    COMMIT;
<span class="key">ELSE</span>
    <span class="key">ROLLBACK</span>; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is a Pivot Table? Give basic example</strong></h3>



<p class="custp1 p-mb-0">A <strong>pivot table</strong> in SQL is a data summarization tool that transforms rows into columns, allowing you to reorganize and aggregate data for easier analysis. It helps to summarize and analyze large datasets by grouping and displaying data in a cross-tabular format.</p>



<div style="padding-top: 0.625rem; padding-bottom: 0.3125rem;"><b><i><u>example:</u></i></b></div>



<pre class="pchl"><code><span class="key">SELECT</span> *
<span class="key">FROM</span> (
    <span class="key">SELECT</span> Month, Product, Sales
    <span class="key">FROM</span> SalesData
) <span class="key">AS</span> SourceTable
PIVOT (
    SUM(Sales)
    <span class="key">FOR</span> Month <span class="key">IN</span> ([Jan], [Feb], [Mar], [Apr])
) <span class="key">AS</span> PivotTable; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>What is SQL injection?</strong></h3>



<p class="custp1"><strong>SQL injection</strong> is a type of cyber attack where an attacker inserts malicious SQL code into an input field, potentially allowing them to manipulate the database and access unauthorized data.</p>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>How to Prevent SQL Injection</strong></h3>



<br>



<ol class="ol1 wp-block-list">
<li><strong>Use Prepared Statements</strong>: Parameterize queries to separate SQL code from data.</li>



<li><strong>Use Stored Procedures</strong>: Encapsulate SQL queries within stored procedures.</li>



<li><strong>Validate Input</strong>: Ensure that input data is properly validated and sanitized.</li>



<li><strong>Limit Database Permissions</strong>: Restrict user permissions to only what is necessary.</li>



<li><strong>Use ORM Libraries</strong>: Object-Relational Mapping libraries often handle parameterization automatically.</li>
</ol>



<br><br>



<h3 class="wp-block-heading hLBRed"><strong>Write syntax to create a stored procedure.</strong></h3>



<pre class="pchl"><code><span class="key">CREATE</span> <span class="key">PROCEDURE</span> GetEmployeesByDepartment
    @DeptID INT
<span class="key">AS</span>
<span class="key">BEGIN</span>
    <span class="key">SELECT</span> * 
    <span class="key">FROM</span> Employees 
    <span class="key">WHERE</span> DepartmentID = @DeptID;
<span class="key">END</span></code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to prevent duplicate records when making a query?</strong></strong></h3>



<br>



<p class="p-mb-0"><strong>1)</strong> Using the&nbsp;<span class="spanHT">DISTINCT</span>&nbsp;statement with <span class="spanHT">SELECT</span></p>



<pre class="pchl"><code><span class="key">SELECT <b>DISTINCT</b></span> column1, ... <span class="key">FROM</span> table_name </code></pre>



<p class="custp1 p-mb-0"><strong>2)</strong> Using <span class="spanHT">GROUP BY</span></p>



<pre class="pchl"><code><span class="key">SELECT</span> FirstName, LastName
<span class="key">FROM</span> Employees
<span class="key"><b>GROUP BY</b></span> FirstName, LastName; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>How to find the last ID (number) in a table? </strong></h3>



<br>



<p class="p-mb-0"><strong>1)</strong> Using the <span class="spanHT">MAX()</span> function.</p>



<pre class="pchl"><code><span class="key">SELECT</span> <span style="color:#FF33CC; background:#f2f19d;"><b>MAX</b>(ID)</span> <span class="key">AS</span> LastID <span class="key">FROM</span> TableName; </code></pre>



<p class="custp1 p-mb-0"><strong>2)</strong> Using <span class="spanHT">TOP</span> and <span class="spanHT">ORDER BY</span></p>



<pre class="pchl"><code><span class="key">SELECT <span style="background:#f2f19d;"><b>TOP</b></span></span> 1 ID <span class="key">FROM</span> TableName <span class="key" style="background:#f2f19d;"><b>ORDER BY</b></span> ID DESC; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>How to find 6th highest salary</strong></h3>



<br>



<p class="p-mb-0">You can use the <span class="spanHT">ROW_NUMBER()</span> function</p>



<pre class="pchl"><code><span class="key">SELECT</span> Salary
<span class="key">FROM</span> (
    <span class="key">SELECT</span> Salary, <span style="background:#f2f19d;"><b>ROW_NUMBER()</b></span> <span class="key">OVER</span> (ORDER <span class="key">BY</span> Salary <span class="key">DESC</span>) <span class="key">AS</span> RowNum
    <span class="key">FROM</span> Employees
) <span class="key">AS</span> RankedSalaries
<span class="key">WHERE</span> RowNum = 6; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong><strong>How to find the values in a text column of a table that start with a certain letter?</strong></strong></h3>



<br>



<p class="p-mb-0">Use <span class="spanHT">LIKE</span> operator with <span class="spanHT">%</span></p>



<pre class="pchl"><code><span class="key">SELECT</span> * <span class="key">FROM</span> Employees <span class="key">WHERE</span> Name <span style="background:#f2f19d;"><span class="key">LIKE</span> 'G%'</span> </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>How to select random rows from a table?</strong></h3>



<br>



<p class="p-mb-0">Using the <span class="spanHT">NEWID()</span> function</p>



<pre class="pchl"><code><span class="key">SELECT</span> * <span class="key">FROM</span> TableName <span class="key">ORDER BY</span> <span style="background:#f2f19d;"><span style="color:#FF33CC;"><b>NEWID</b><span>()</span>; </code></pre>



<br><br><br>



<h4 class="wp-block-heading hLBRed"><strong>How can you create a new table with the same structure as an existing table?</strong></h4>



<br>



<p class="p-mb-0">Using the <span class="spanHT">INTO</span> operator with a <span class="spanHT">WHERE</span> clause that is always false for all records.</p>



<pre class="pchl"><code><span class="key">SELECT</span> * <span style="background:#f2f19d;"><span class="key">INTO</span> Employee_copy</span>
<span class="key">FROM</span> Employee <span class="key">WHERE</span> 1 = 2; </code></pre>



<br><br><br>



<h3 class="wp-block-heading hLBRed"><strong>How can database performance be improved?</strong></h3>



<p class="custp1 p-mb-0">Measures to Increase Database Performance:</p>



<ul class="ol1 wp-block-list">
<li><strong>Indexing:</strong> Create indexes on frequently queried columns to speed up searches.</li>



<li><strong>Query Optimization:</strong> Write efficient SQL queries, avoid unnecessary columns in SELECT, and use proper JOINs.</li>



<li><strong>Normalization:</strong> Normalize data to reduce redundancy and improve data integrity.</li>



<li><strong>Denormalization:</strong> Use denormalization where necessary to reduce JOIN operations and improve read performance.</li>



<li><strong>Partitioning:</strong> Split large tables into smaller, manageable pieces (partitions) for faster access.</li>



<li><strong>Caching:</strong> Use caching mechanisms to store frequently accessed data in memory.</li>



<li><strong>Load Balancing:</strong> Distribute database load across multiple servers.</li>



<li><strong>Hardware Upgrades:</strong> Upgrade to faster CPUs, add more RAM, and use SSDs for better performance.</li>



<li><strong>Regular Maintenance:</strong> Perform routine maintenance like updating statistics, rebuilding indexes, and cleaning up logs.</li>



<li><strong>Connection Pooling:</strong> Use connection pooling to reuse database connections efficiently.</li>



<li><strong>Database Configuration:</strong> Optimize database configuration settings for performance.</li>



<li><strong>Monitoring and Profiling:</strong> Monitor database performance and profile queries to identify and address bottlenecks.</li>
</ul>



<br><br>



<style>
.shbtn {
    color: #fff;
    padding: 6px 12px;
    margin-top: 8px;
}
</style>



<br>
<div id="download-questions" style="border: solid 1px lightblue; padding: 10px;">
<button class="shbtn" style="float: right; border-radius: 20px;" onclick="saveQuestionsAsPDF()">Download</button>
<div><span style="font-weight: bold; font-size: 20px; color: #7100e2;">Download all Questions as .PDF</span></div>
<div id="questions"><br><br><center><span style="color:#808080;"><i>loading&#8230;</i></span></center></div>
</div>



<br>
<div id="PageInAd1"></div>
<script>
fetch('/gads/PageInAd1.txt')
	.then(response => response.text())
	.then(text => {
		document.getElementById('PageInAd1').innerHTML = text;
	})
	.catch(error => {
		console.error('Error fetching manual PageInAd1:', error);
	});
</script>



<br><br>
<script src="/my-js/latesttop10post.js" type="text/javascript"></script>
<input type="hidden" id="cids" value="3,59,71">
<div id="latestPostlist"></div>
<br>



<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.min.js"></script>



<script>
window.onload = function (){
	/*Sl.No for H3*/
	var h3arr = document.getElementsByClassName("hLBRed");
	var questions = document.getElementById("questions");
	questions.innerHTML = "<br><br><div><h2><center><b><u>SQL Interview Questions</u></b></center></h2></div><br>";
	var count = 1;
	var searchResult = "";
	for (h3 of h3arr) {
		questions.innerHTML = questions.innerHTML + "<div>" + "<b>" + count + ")</b> " + h3.innerText.replace(/[^\w\s#?.,=<>]/gi, '').replace('<','&lt;').replace('>','&gt;') + "</div>";
		h3.innerHTML = "<b>" + count + ") " + h3.innerHTML + "</b>";
		h3.id = "q" + count;
		searchResult += '<div class="filter-list-item"><a href="#' + h3.id + '">' + h3.innerText + '</a></div>'
		count++;
	}

	document.getElementById('myList').innerHTML = searchResult;
};


/* Print Questions */
function saveQuestionsAsPDF(divId, title) { 
	if (document.getElementById("questions").innerText.trim().length == 0) return;

	var doc = new jsPDF();
	doc.fromHTML('<html><head></head><body><div>https://www.codeindotnet.com/all-interview-questions-and-answers/</div>' + document.getElementById("questions").innerHTML + '<br><br><br><div><b>See Other Questions &amp; Answers:</b> https://www.codeindotnet.com/all-interview-questions-and-answers/</div></body></html>');
	doc.save('sql-interview-questons.pdf');
}

</script>



<script type="text/javascript">
        document.getElementById("myInput").addEventListener("keyup",
    		function searchFilter() {
    			// Declare variables
    			var input, filter, div, divs, a, i;
    			input = document.getElementById('myInput').value.replace(/\s{2,}/g, ' ').trim().toUpperCase();
				arrFilters = input.split(' ');
    			div = document.getElementById('myList');
    			divs = div.getElementsByClassName('filter-list-item');
				
    			if (document.getElementById("myList").style.display == "none"){
    				document.getElementById("myList").style.display = "block";
    			}

				if(input.length == 0){
					for (i = 0; i < divs.length; i++) {
						divs[i].style.display = '';						
					}
				}
				else {
					// Loop through all list items, and hide those who don't match the search query
					for (i = 0; i < divs.length; i++) {
						a = divs[i].getElementsByTagName('a')[0];
						divs[i].style.display = 'none';
						for (j = 0; j < arrFilters.length; j++) {
							if ((arrFilters[j].length > 0) && (a.innerHTML.toUpperCase().indexOf(arrFilters[j]) > -1)) {
								divs[i].style.display = '';
								break;
							} 						
						}
						
					}
				}
    
		});
    </script>



<script>
function displayAllResult(){
	document.getElementById("myInput").value = ''
	div = document.getElementById('myList');
	divs = div.getElementsByClassName('filter-list-item');
	for (i = 0; i < divs.length; i++) {
		divs[i].style.display = '';						
	}
}
</script>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codeindotnet.com/sql-interview-questions-answers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
