Professional Documents
Culture Documents
SQL Introduction
SQL stands for Structured Query Language. It is mainly used for data manipulation,
data modification, and data retrieval. This comes around with Relational Database
Management System (RDBMS).
We will learn about more advanced features of SQL like Joins and Functions.
How To Work With SQL Joins
A simple join means is to combine two or more tables in a given database. A join
works on a common entity of two tables.
A join contains 5 sub-joins which as; Inner join, Outer Join, Left Join, Right Join and
Cross Join.
Inner Join
An inner join is used to select records that contain common or matching values in
both the tables (Table A and Table B). Non-matching are eliminated.
So, let’s understand the type of joins, and with common examples and the
differences between them.
Note: JOIN or INNER JOIN means the same. But always better to use INNER JOIN,
and this specifies your intention explicitly.
Output: Now the final output table will look like this;
If you look at the output window, we have got only 8 rows, but in the table
tblEmployee, we have 10 rows. We didn’t get JAMES and RUSSELL records. This is
because the DEPARTMENTID, in table tblEmployee is NULL for these two
employees and does not match with their ID column in table tblDepartments.
So, in a final statement, Inner Joins return only matching rows from both the tables
and non-matching rows are eliminated due to its subquery.
Left Join
LEFT Join returns all the matching rows and non-matching rows from the left side
table. In addition, Inner join and Left join are extensively used each other.
So, let’s take an example, I want all the rows from the tblEmployee table,
including JAMES and RUSSELL records. Then the output will look like as;
Right Join
RIGHT Join returns all the matching rows and non-matching rows from the right side
table.
So, let’s take an example; I want all the rows from right tables involved in the join. As
a result would be like;
Full Outer Join
OUTER join or FULL OUTER Join returns all rows from both the left and right tables,
and including the non-matching rows from the tables.
So, let’s take an example; I want all the rows from both the tables involved in the
join.
Cross Join
This join gives the Cartesian product of the 2 tables in the join function. This join
does not contain ON clause.
So, let’s understand an example: In the tblEmployee table we have 10 rows and in
the tblDepartments table we have 4 rows. So, a Cross joins between these 2 tables
produces 40 rows.
How To Work With Advance SQL Joins
So, let’s understand an example, I want to retrieve only the non-matching rows from
the left side table.
Output: Finally, the output will look like this;
Right Join
So, let’s understand an example, I want to retrieve only the non-matching rows from
the right side table.
Output: Finally, the output will look like this;
So, let’s understand an example, I want to retrieve only the non-matching rows from
the right side table and left side table and matching rows should be eliminated.
A Key in SQL is a data field that exclusively identifies a record. In another word, a
key is a set of column(s) that is used to uniquely identify the record in a table.
1.Candidate Key
2.Primary Key
3.Unique Key
4.Alternate Key
5.Composite Key
6.Super Key
7.Foreign Key
Before going ahead, and please have a look at the image below;
Let’s understand each key in details
Candidate Key
A candidate key is a key of a table which can be selected as a primary key of the
table and a table can have multiple candidate keys, therefore one can be selected as
a primary key.
Primary Key
A primary key is similar to selected candidate key of the table to verify each data
record uniquely in the table. Therefore, primary key does not contain any null value
in any of the columns of a table and it also keeps unique values in the column. In the
given example, Employee_Id defines primary key of Employee table. Consequently,
in SQL Server Management Studio, Primary key creates a clustered index on a heap
table by default and a table which does not consist a clustered index is known as a
heap table. Whether defines a nonclustered primary key on a table by type of index
explicitly.
Furthermore, a table can have only one primary key and primary key can be defined
in SQL Server using SQL statements:
1.CRETE TABLE statement (at the time of table creation) – as a result, the system
Unique Key
A unique key is much as the primary key and which does not contain duplicate
values in the column. It has below differences in the comparison of the primary key:
Alternate Key
The alternate key is similar to candidate key, But not selected as a primary key of the
table.
Composite Key
Super Key
Super key is a set of columns on which all columns of the table are functionally
dependent. Due to the set of columns that uniquely identifies each row in a table. In
another word, this key holds few additional columns which are not strictly required to
uniquely verify each row in the table. Seems like, Primary key and candidate keys
are minimal superkeys or you can say a subset of superkeys.
So, let’s look at above example, In the Employee table, column name Employee_Id
is hardly sufficient to uniquely verify any row of the table. So, that any set of a
column from Employee table which contains Employee_Id is a superkey for
Employee Table.
License_Number and Passport_Number are the columns name, it can also uniquely
verify any of row of the table. Anyone of column name set which consists
License_Number or Passport_Number or Employee_Id is a superkey of the table.
Foreign Key
An FK defines the relationship between two or more than two tables at a time. A
primary key of a single table is referred to a foreign key in another table. A foreign
key can have duplicate values in a table and also it can have null values if the
column name is defined to accept null values yet.
Note: Keys like primary key and unique key creates indexes with keys columns.
Organized data in B-Tree structure node (Balanced Tree: Leaf nodes are all at the
different level from the root side) in SQL Server. Hence, Nonclustered index creates
a separate structure from the base data table but clustered index converts base data
table from heap structure to a B-Tree structure.
In addition, the clustered index does not create a separate structure apart from the
base table and that is the reason we can create only one clustered index on a table.
Hence, we can sort a table in only one way (it may have multiple columns to sort but
sorting can be done in one only way) which is the order of the clustered index.
A Function is an entity program which is stored in the SQL Server Database either
you can pass parameters into or return a value. Furthermore, we will look forward to
some very useful Built-In function and User-defined functions.
Coalesce Function
Coalesce() : This function return only coming first Non NULL value. So, let’s take an
example over Coalesce() function.
Let’s understand the table as name ‘Employee’ above. As a result, you can see
some of the employees have their First name missing, some have a Middle name
and some of them have Last Name missing. So, I want to return only “Name” of the
employee.
LEFT() Function
LEFT(Character_Expression, Integer_Expression) – This function returns the
specified number of characters from the left-hand side of the given character value
expression.
Example: Select LEFT(‘ABCDE’, 3)
Output: ABC
RIGHT() Function
RIGHT(Character_Expression, Integer_Expression) – This function returns the
specified number of characters from the right-hand side of the given character value
expression.
Example: Select RIGHT(‘ABCDE’, 3)
Output: CDE
CHARINDEX() Function
CHARINDEX(‘Expression_To_Find’, ‘Expression_To_Search’, ‘Start_Location’) –
This functions returns the starting position of the specified value expression in a
character sub string. Start_Location parameter is optional.
Example: Let’s understand, we make the starting position of ‘@’ character in the
email string ‘sara@aaa.com’.
Select CHARINDEX(‘@’,’sara@aaa.com’,1)
Output: 5
SUBSTRING() Function
SUBSTRING(expression’, ‘Start’, ‘Length’) – This function returns substring (subpart
of the string), from the given value expression. In addition, when you specify the
starting position using the ‘start’ parameter and the other number of characters in the
substring using ‘Length’ parameter. All three of the parameters are mandatory.
Example: I want to display just domain part of the given email ‘John@bbb.com’.
Select SUBSTRING(‘John@bbb.com’,6, 7)
Output: bbb.com
As a result, we made the coding with the starting position and the length parameters.
Instead of hardcoding the parameters, we can dynamically fetch them
using CHARINDEX() and LEN() string functions as shown below.
Example:
Select SUBSTRING(‘John@bbb.com’,(CHARINDEX(‘@’, ‘John@bbb.com’) + 1),
(LEN(‘John@bbb.com’) – CHARINDEX(‘@’,’John@bbb.com’)))
Output: bbb.com
So, let’s take an real example with the use of LEN(),
CHARINDEX() and SUBSTRING() functions. Let’s think we have a table as shown
below;
So, the question is How will you find the total number of emails by their domain.
REPLICATE() Function
REPLICATE(String_To_Be_Replicated, Number_Of_Times_To_Replicate) – This
function repeats the given point of the string, and for the specified number of times.
Example: SELECT REPLICATE(‘Pragim’, 3)
Output: Pragim Pragim Pragim
Let’s talk about a practical example of using REPLICATE() function: We will be using
this table most of the time, and for the rest of our examples in this article.
So, let’s suppose we have a table as shown below;
Let’s make email with 5 * (star) symbols. Then, the output would be like this
SPACE() Function
SPACE(Number_Of_Spaces) – This function returns the only number of spaces, and
specified by the term Number_Of_Spaces argument.
Example: The SPACE(5) function, It will inserts 5 spaces between FirstName and
LastName
Select FirstName + SPACE(5) + LastName as FullName From tblEmployee
PATINDEX() Function
This function only returns the starting location of the first occurrence of a pattern in a
specified effective expression. Hence, it takes only two arguments, and the pattern to
be searched and the expression. Therefore, PATINDEX() is similar
to CHARINDEX(). With CHARINDEX() we cannot use wildcards, while PATINDEX()
involves this capability. If the specified pattern value is not found, PATINDEX()
returns ZERO.
Example: Select Email, PATINDEX(‘%aaa.com, Email’) as FirstOccurence from
tblEmployee where PATINDEX(‘%@aaa.com’, Email) > 0
REPLACE() Function
REPLACE(String_Expression, Pattern, Replacement_Value), This function replaces
all occurrences position of a specified string value with another string value.
Example: All .COM strings are replaced with .NET
Select Email, REPLACE(Email, ‘.com’, ‘.net’) as ConvertedEmail from tblEmployee
STUFF() Function
STUFF(Original_Expression, Start, Length, Replacement_expression), This STUFF()
function only inserts Replacement_expression, which is specified at the starting
position, along with removing the characters specified using Length parameter value
expression.
Example: Select FirstName, lastName, Email, STUFF(Email,2,3,’*****’) as
StuffedEmail from tblEmployee.
There are several built-in DateTime functions available in SQL Server database.
Most of the following functions can be used to get the current system date and time,
and where you have SQL server installed.
Hence, UTC stands for Coordinated Universal Time, based on which, the world
regulates clocks and time data. Noteworthy. there are minor differences between
GMT and UTC, but for most common purposes, UTC is synonymous across with
GMT.
IsDate() Function
ISDATE() – This function checks if the only given value, and exists a valid date, time,
or DateTime. Then, it will return 1 for success, 0 for failure.
Example:
Select ISDATE(‘PRAGIM’) — it will returns 0
Example:
Select ISDATE(Getdate()) — it will returns1
Example:
Select ISDATE(‘2018-01-20 21:02:04.167’) — it will returns 1
Note: For datetime2 values, IsDate returns ZERO.
Example:
Select ISDATE(‘2018-01-20 22:02:05.158.1918447’) — it will returns 0.
Day() Function
Day() – This function only returns the ‘Day number of the Month’ of the given date.
Examples:
Select DAY(GETDATE()) — It will give the output on behalf of the day number of the
month, and based on current system DateTime.
Select DAY(’01/14/2018′) — it will returns 14
Month() Function
Month() – This function will give the output on behalf of the ‘Month number of the
year’ of the given date.
Examples:
Select Month(GETDATE()) — This function will give the output on behalf of the
‘Month number of the year’, and based on the current system date and time.
Select Month(’05/14/2018) — it will returns 5
Year() Function
Year() – This function will give the output on behalf of the ‘Year number’ of the given
date
Examples:
Select Year(GETDATE()) — Returns the year number, and based on the current
system date
Select Year(’01/20/2018) — it will returns 2018
DateName() Function
DateName(DatePart, Date) – This function returns only a string expression, and that
only represents a part of the given date. These functions consist 2 parameters.
The first parameter ‘DatePart’ specifies, the part of the date, that we want. The
second parameter is the real date, from which we want the part of the Date.
Example 1:
Select DATENAME(Day, ‘2017-04-20 13:47:47.350’) — it will returns 20
Example 2:
Select DATENAME(WEEKDAY, ‘2017-04-20 13:47:47.350’) — it will return Thursday
Example 3:
Select DATENAME(MONTH, ‘2017-04-20 13:47:47.350’) — it will returns April
So, let’s take an example using some of these DateTime functions. Consider the
table tblEmployees.
DatePart() Function
DatePart(DatePart, Date) – This function gives an integer representing the specified
DatePart value. Mostly function is similar to DateName() function. DateName() only
returns nvarchar value, while DatePart() returns only an integer value. The valid
DatePart parameter values are shown below.
Examples:
Select DATEPART(weekday, ‘2012-08-30 19:45:31.793’) — it will returns 5
Select DATENAME(weekday, ‘2012-08-30 19:45:31.793’) — it will returns Thursday
DateAdd() Function
DATEADD (datepart, NumberToAdd, date) – This SQL function gives only the
DateTime, after specified term NumberToAdd, and to the datepart specified of the
given date.
Examples:
Select DateAdd(DAY, 10, ‘2018-01-20 19:45:31.793’) — it will returns ‘2018-01-30
19:45:31.793’
Select DateAdd(DAY, -10, ‘2012-08-30 19:45:31.793’)– it will returns ‘2018-01-20
19:45:31.793’
DatedDiff() Function
DATEDIFF(datepart, startdate, enddate) – This function gives the count of the
specified datepart boundaries crossed among the specified startdate and enddate.
Examples:
Select DATEDIFF(MONTH, ’11/30/2005′,’01/31/2006′) — it will returns 2
Select DATEDIFF(DAY, ’11/30/2005′,’01/31/2006′) — it will returns 62
So, let’s take an example, Let’s suppose we have a table given below;
So, Write a query to find out the age of a person, when the date of birth is given.
Finally, the output will look as shown below.
So, let’s have a look at practical example with help of CAST() function;
1.Scalar functions
Scalar Functions
Scalar functions vary in parameters that may or may not have parameters, and
always gives a single (scalar) value in the output. Therefore, the returned value can
be of any data type format except text value, text, image, cursor, and timestamp.
Example: So, let’s develop a function which calculates and returns the age of a
person in output. Consequently, to compare the age we required for, date of birth.
So, let’s pass date of birth as a parameter. Therefore, AGE() function will return an
integer and will accept date parameter.
Select dbo.Age( dbo.Age(’10/08/1982′).
Scalar user-defined functions can be used in the Select clause as shown below.
Employee Table
Multi-statement Table Valued function(MSTVF):
Conclusion
The JOINs is very much understanding term for beginners during the learning phase
of SQL commands. Consequently, In the interview, Interviewer asks at least one
question is about the SQL joins, and functions. So, in this post, I am trying to simplify
the things for new SQL learners and make it easy to understand the SQL joins.
Furthermore, The functions in SQL, A lot of people are having trouble to understand
actual working function. Because SQL contains a lot of data in bulk in different
database and table names. A Function is a stored program in the SQL Server
Database where you can pass parameters into and return a value. So, I have given
some more useful term about working of Functions.
Connect with source url:-
https://www.loginworks.com/blogs/use-joins-
functions-sql/