Skip to main content

Posts

VAR function in SQL Server

The VAR is an aggregate function in SQL Server which returns the variance of all or distinct values of the specified column in SQL Statement.  Let's look into the syntax, Syntax SELECT VAR ( [ ALL | DISTINCT ] expression ) FROM [Table_Name] ALL - It is default. Returns the variance of all values. DISTINCT - Returns the variance of unique values ALL SELECT VAR(ALL ProductID) as ALLProduct FROM [AdventureWorks2019].[Production].[ProductInventory] The above statement returns the variance of ALL product values. DISTINCT SELECT VAR(DISTINCT ProductID) as DistinctProduct FROM [AdventureWorks2019].[Production].[ProductInventory] The above statement returns the variance of DISTINCT  product values. To learn more - Click Here

COUNT_BIG function in SQL Server

The COUNT_BIG is an aggregate function in SQL server which returns the number of items within a group. This function works like COUNT function but the only difference is the data type of its return values. The COUNT_BIG function return type is bigint , COUNT function return type is int .  Syntax COUNT_BIG ( { [ [ ALL | DISTINCT ] expression ] | * } ) ALL  - It is the default in COUNT_BIG function. This applies the aggregate function to all values. DISTINCT - This returns the number of unique nonnull values. Expression - It is an expression of any type but does not support aggregate functions or subqueries. * - This denotes that all the rows should be counted, including the duplicate rows and null values and returns the total number of rows in a statement. Let's look into an example, SELECT COUNT_BIG(*) AS "Work Order Count" FROM [AdventureWorks2019].[Production].[WorkOrder] Result: ALL SELECT COUNT_BIG(ALL WorkOrderID) AS "Work Order Count" FROM [Adventur...

APPROX_COUNT_DISTINCT function in SQL Server

APPROX_COUNT_DISTINCT is an aggregate function in SQL Server which returns the approximate number of distinct values. This is very efficient with large number of rows.  Syntax APPROX_COUNT_DISTINCT (expression) The return types of this function is bigint . And expression type can be of any type, except image, sql_variant, ntext, or text - as per Microsoft documentation .  Let's look into an example, SELECT APPROX_COUNT_DISTINCT([ProductID]) AS Approx_Distinct_Product FROM [AdventureWorks2019].[Production].[WorkOrder]; The above statement returns the approximate number of different products from WorkOrder table. This is very powerful with billions of rows without any high memory utilization or performance issues.

Row Number function in SQL

The ROW_NUMBER() function assigns the integer value to each row of the returned result set which starts at 1 and increments by 1. Let's look into the syntax, ROW_NUMBER() OVER ( [PARTITION BY exp1, exp2 ... expn] ORDER BY col1,col2...coln) The ROW_NUMBER function must have an OVER clause with ORDER BY. ORDER BY clause is mandatory PARTITION BY clause is optional Let's look into an example, SELECT TOP 10 [PersonType] ,[FirstName] ,[LastName] , ROW_NUMBER() OVER(ORDER BY FirstName) as RowNum FROM [AdventureWorks2019].[Person].[Person] For the above example, we are using the [Person] table from [AdventureWorks2019] database. This statement returns the temporary row number for each row based on the order of first name column. Using ROW_NUMBER function with PARTITION BY clause The PARTITION BY clause breaks up the result set into partitions. Once the data is partitioned, row number is reset to 1 when the partition changes. SELECT [ProductID] ,[Name] ...

COL_NAME function in SQL Server

COL_NAME is a predefined SQL server function that returns the name of a table column using table and column identification numbers. COL_NAME (table_id, column_id) COL_NAME function takes two parameters which is table and column identification numbers like specified in the above statement. Let's look into an example of AdventureWorks2019 database, SELECT TOP (1000) [BusinessEntityID] ,[PersonType] ,[NameStyle] ,[Title] ,[FirstName] ,[MiddleName] ,[LastName] ,[Suffix] ,[EmailPromotion] ,[AdditionalContactInfo] ,[Demographics] ,[rowguid] ,[ModifiedDate] FROM [AdventureWorks2019].[Person].[Person] Here is the above statement result set, Let's see how to retrieve the column name FirstName using the COL_NAME function, SELECT COL_NAME(OBJECT_ID('Person.Person'), 5) AS ColumnName; /* COL_NAME (table_id, column_id) */ The OBJECT_ID function returns the identification number of a specified tab...

CONTAINS Predicate in SQL

CONTAINS is a predicate used to perform a full-text search in one or more full-text indexed columns. Let's look into an example, SELECT [Description] FROM [Sales].[SpecialOffer] WHERE CONTAINS([Description], 'discount'); /* CONTAINS(Column_Name, Search_Text) */ The above statement returns the data which has  discount  word in column  [Description] .  And CONTAINS predicate is not case sensitive. CONTAINS text search is very quick and easy to search a word or phrase.   Note: CONTAINS full-text search works in a full-text indexed column. If you face any errors, have a look at this link here . Using CONTAINS with <multiple columns> SELECT [Description],[Type] FROM [Sales].[SpecialOffer] WHERE CONTAINS(([Description],[Type]), 'discount'); /* CONTAINS((Column_Name1,Column_Name2,...), Search_Text) */ The above CONTAINS query searches the word discount in both [Description] and [Type] columns and returns the result that matches the exact word. Using C...

IS NOT NULL SQL

 IS NOT NULL is the opposite of the IS NULL predicate, which is useful to retrieve the values that doesn't have NULL value in an expression. Let's look into an example of AdventureWorks2019 database, SELECT [BusinessEntityID] ,[DepartmentID] ,[ShiftID] ,[StartDate] ,[EndDate] ,[ModifiedDate] FROM [AdventureWorks2019].[HumanResources].[EmployeeDepartmentHistory] WHERE [EndDate] IS NOT NULL Above statement returns the  EmployeeDepartmentHistory which doesn't have NULL EndDate values.

IS NULL SQL Predicate

 IS NULL is a predicate which is useful to identify the expression has NULL values.  Let's look into an example of AdventureWorks2019 database, SELECT [BusinessEntityID] ,[DepartmentID] ,[ShiftID] ,[StartDate] ,[EndDate] ,[ModifiedDate] FROM [AdventureWorks2019].[HumanResources].[EmployeeDepartmentHistory] WHERE [EndDate] IS NULL Above statement returns the EmployeeDepartmentHistory which has NULL EndDate value.

Cannot use a CONTAINS or FREETEXT predicate on table or indexed view because it is not full-text indexed - [Solved]

 To resolve this issue, we need to enable the full-text indexing on table. Follow the steps below to resolve the issue. Step1: Go to your table and right-click and choose Full-Text index option and select Define Full-Text Index Step2: Choose next in Full-text indexing wizard Step3: Click next (A unique constraint will be created automatically) Step4: Choose the column which you want full-text indexing then click next Step5: Click next Step6: Click next again Step7: Click next Step8: Click Finish - Your full-text indexing will be ready Step9: TADA! Step10: Resolved!!!

TABLESAMPLE in SQL

What is TABLESAMPLE? It is a clause used in the SQL SELECT statement to retrieve the random number of data from a table without any specific order. There are three ways to achieve this, By Percentage By No of rows By Seed number By Percentage TABLESAMPLE percentage will return the percentage of the data pages of a table. Let's look into an example of the  AdventureWorks2019 database with the customer table of 19,820 rows. SELECT *FROM Sales.Customer TABLESAMPLE(10 PERCENT); You may assume that TABLESAMPLE will return the 10 percentage of total rows which is 19,820 but it doesn't. It will return the percentage of data pages not total number of rows. Let's look into an below example, Example 1: Example 2: As you can see in the above examples, when we executed the query in the first example returned 3,135 rows and the second example returned 2,475 rows. TABLESAMPLE will return the percentage of data pages not the percentage of total rows of a table. By No of rows SELECT ...

How to format query results to JSON?

By using FOR JSON clause we can simply format the query results to JSON output.  Let's look into an example, SELECT Id, ProductNo, ProductName from tbl_products FOR JSON AUTO; In the above statement, we have three columns (Id, ProductNo, ProductName) from the Products table with FOR JSON clause and AUTO mode to automatically return the JSON formatted output based on the order of SELECT statement. Output [ { "Id":1, "ProductNo":2, "ProductName":"Alexa Eco Dot" }, { "Id":2, "ProductNo":3, "ProductName":"Alexa Eco Dot1" }, { "Id":3, "ProductNo":4, "ProductName":"Alexa Eco Dot2" }, { "Id":4, "ProductNo":5, "ProductName":"Alexa Eco Dot3" } ]

How to create a sequence in SQL?

Sequence is a unique set of numerical values 1, 2, 3, ... which are generated based on needs.  CREATE SEQUENCE sequence_name START WITH integer_value INCREMENT BY integer_value MINVALUE minimum_value MAXVALUE maximum_value CYCLE|NOCYCLE sequence_name : Unique name of the sequence. minimum_value: Minimum value of the sequence. maximum_value: Maximum value of the sequence. cycle: It continues the same sequence once it reaches the maximum value. Basically, once the maximum value is reached it resets the next value as minimum value. nocycle: Throws an error message when it reaches the maximum value. Example Let's create a sequence named "prod_sequence" like mentioned in below statement, CREATE SEQUENCE prod_sequence START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 5 NO CYCLE After the sequence has been created to see the properties of the sequence use below statement, SELECT * FROM sys.sequences WHERE name = 'prod_sequence'; Using Sequence The below...

Bootstrap 5 Toasts

Toast is a lightweight push notification used to provide hints to the users for the consequence of any user actions (eg. Form submission, Notification alert messages and etc.) Default Toasts have two parts basically, one is the header ( .toast-header ) and another is the body ( .toast-body ) which contains the actual message. See the Pen Bootstrap 5 Toasts by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Colorful Toasts  You can change the background color of the Toasts by using the Bootstrap 5 background utilities (eg.  .bg-primary , .bg-secondary , .bg-info , .bg-danger , etc...). See the Pen Bootstrap 5 Colorful Toasts by Prime Study Hub ( @PrimeStudyHub ) on CodePen .

Remove Duplicates from JS array - [Solved]

Here we are gonna see how to remove duplicates from JS array in either of the below format, Duplicates in an Array (or) Duplicates in an Array of Objects Let's go through each in detail. Duplicates in an Array Assume you have duplicates in a list of the country array, var countries=["China","India","United States","Indonesia", "India","Pakistan" ]; As you can see in the above array, country India is repeating twice from the list. In order to get an accurate unique country list, you can use any of the following methods. jQuery Hashtables Sort jQuery See the Pen Remove Duplicates in an array using jQuery by Prime Study Hub ( @PrimeStudyHub ) on CodePen . This method loops through your original array and finds the duplicate items and removes them and updates the non-duplicate items into the new array. Hashtable See the Pen Remove Duplicates in an array using Hashtable by Prime Study Hub ( @Prim...

What is Bootstrap 4?

The Bootstrap 4  is the newest version of  Bootstrap , which is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first web sites. The new features are, Bootstrap 4 Source CSS files Converted in SCSS. Bootstrap 4 has 5 grid system ( .col-, .col-sm-3, .col-md-3, .col-lg-3, .col-xl-3 ).  Bootstrap 4 has removed the xs from the lowest breakpoint. Therefore, ( col- ) covers all devices. Bootstrap 4 has changed the offset class ( .offset-md-4 ) In Bootstrap 3 we are used this ( .img-responsive ) class for the responsive image.  Bootstrap 4 has changed the class for a responsive image ( .img-fluid ). Bootstrap 4 has changed to ( .form-control-lg ) and ( .form-control-sm ) to increase and decrease the input size.  To learn more click here . 

Bootstrap 4 Spinners

Bootstrap 4 Spinners The Spinner is a major component of a website that ensures all necessary libraries, programs, contents, and images are loaded, which is essential during the startup phase of running a website. Let's see how to create a Bootstrap 4 Spinner? In order to create lightweight bordered spinner, include .spinner-border class to your element. <div class="container">     <div class="spinner-border"></div> </div> Demo See the Pen Bootstrap 4 Spinners by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Color Variations You can create spinners with different color variations like below example, See the Pen Bootstrap 4 Colored Spinners by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Growing Spinners In order to create a growing spinner instead of border spinner, Use .spinner-grow class along with text color utilities to change its appearance. Let's look into a detailed example, See the Pen B...

Top 10 Bootstrap Interview Questions

Top 10 Bootstrap Interview Questions In this article, you will learn about the top 10 most frequently asked interview questions and their answers. What is Bootstrap? Bootstrap is an open-source HTML, CSS, and Javascript framework for responsive & mobile-friendly website designing.  There are 3 elements in bootstrap, bootstrap.css - a CSS framework bootstrap.js - a Javascript/JQuery framework glyphicons - font & icon set Bootstrap can be simply integrated along with distinct other platforms and frameworks, on existing sites and the new ones too. You can also utilize specific elements of Bootstrap along with your current CSS. Bootstrap approaches with popular pre-styled components for alerts, dropdowns, navigation bars, etc. It helps to reduce your project completion time. To learn more click here. What are the advantages of Bootstrap? The advantages of Bootstrap are, Easy to Use Responsiveness Customizable Bootstrap Consistency Support Simple ...

Bootstrap 4 Scrollspy

Bootstrap 4 Scrollspy The bootstrap 4 scrollspy automatically highlights the position of the content based on navigation or list-group components. Here's an example of scrollspy, See the Pen Bootstrap 4 Scrollspy by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Use the nav component to create scrollspy links. If you would like to know how to create nav components click here . To create scrollspy, Include  data-spy="scroll" attribute to your scroll content element then add  data- target="#navbarNav" attribute to specify the target element then add a unique ID to your each content area that should be mentioned in your each navigation items like above code sample. Scrollspy Nav Pills Here's an example of scrollspy with nav pills, See the Pen Bootstrap 4 Scrollspy Nav Pills by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Scrollspy list-group See the Pen Bootstrap 4 Scrollspy with list-group by Prime Study Hub ( @PrimeStu...

Bootstrap 4 Navbar

Bootstrap 4 Navbar The Navbar is shown on each page that the user navigates through the website. The bootstrap 4 nav component provides the different styles of the navbar. It also supports logo branding, easy navigation, quote buttons, and forms, etc..  Let's look into each one with an example. The Navbar completely supports responsiveness depending on various screen sizes. To create default navbar, use .navbar class along with responsive class .navbar-expand-sm|md|lg|xl . To create menu items with the hyperlink, Add .navbar-nav class to your <ul> element and add .nav-item class to your each <li> element inherited by an <a> element with .nav-link class for hyperlink. See the Pen Bootstrap 4 Navbar by Prime Study Hub ( @PrimeStudyHub ) on CodePen . Navbar with Background To create navbar with color variation, include any of the contextual classes ( .bg-primary , . bg-secondary , .bg-warning , .bg-danger , .bg-success , .bg-light , .bg-dark ,...

Bootstrap 4 Navs

Bootstrap 4 Navs The Bootstrap 4 Navs are used to create a navigational menu with different styles. It creates a list with the links for navigation. In this tutorial, we are going to explore the different types of bootstrap 4 navigations. Here's an example of a basic navigational menu, See the Pen Bootstrap 4 Navigation by Prime Study Hub ( @PrimeStudyHub ) on CodePen . To create the basic nav, add the .nav class to your <ul> element, then include .nav-item class to your each <li> element represents nav items and add <a> element inside each <li> element with .nav-link class for their links. Navigation Alignment By default, the navigation menu should be on the left. You can change their alignment either right or center as well. For the right nav, add .nav class along with . justify-content-end class. (e.g.  <ul class="nav justify-content-end"> ) For the center nav, add .nav class along with .justify-content-cent...