You are on page 1of 57

BANSAL INSTITUTE OF ENGINEERING AND TECHNOLOGY

Dr. A.P.J. ABDUL KALAM TECHNICAL UNIVERSITY LUCKNOW

IT SKILL LAB 2 (KMBN-251)

For the partial fulfilment of the requirement for the award of the degree of

Batch (2022-2024)
MASTER OF BUSINESS ADMINISTRATION

SUBMITTED TO SUBMITTED BY:


Mr. Puneet Tikkha Rahul Kumar
Assistant Professor Roll no – 2204220700041
Develop pivot table in excel

Before you create a pivot table, organize your data into rows and columns,
and create an Excel Table. There are instructions here.

In this example the source data contains information about food sales, in
two regions -- East and West.

Creating a Pivot Table


After your source data is prepared, you can create a pivot table.

First, follow these steps, to see which pivot table layouts are suggested by
Excel.

1. Select any cell in the source data table.


2. On the Ribbon, click the Insert tab.
3. In the Tables group, click Recommended PivotTables.
4. In the Recommended PivotTables window, scroll down the list, to see
the suggested layouts.
5. Then, click on a layout, to see a larger view.

6. Click on the layout that you want to use, then click OK.

Check the Pivot Table


As soon as you click the OK button, a pivot table is created in your
workbook.

 The pivot table is located on a new sheet


 The pivot table has the layout that you selected from the
Recommended PivotTables dialog box.
 When you select a cell within the pivot table, a PivotTable Field List
appears, at the right of the worksheet.

In the PivotTable Fields pane, you can see that:

 Category is in the Rows area


 Quantity is in the Values area, as Count of Quantity.
 There are no fields in the Filters area or in the Columns area 

Modifying the Pivot Table


You can change the layout of the pivot table, after it's been created.

In this example, we'll add the TotalPrice field to the pivot table.

 In the PivotTable Field List, add a check mark to the TotalPrice field. 
 The TotalPrice field is automatically added to the pivot table, in the
Values area, as Sum of TotalPrice.

Excel added the TotalPrice field to the Values area because it is a number
field.
Change a Value Field Function
When you added the TotalPrice field, Excel automatically added it to the
Values area, and used the Sum function to summarize the prices.

Excel did that because:

 TotalPrice is a number field


 Every row in the source data, for the TotalPrice field, contains a real
number.

However, when the pivot table was created, the Quantity field was added to
the Values area, because it's a number field too. But the Quantity field
shows a count, instead of a sum.

Why did Quantity use the Count function, instead of Sum?

 Although most of the values are numbers, there is at least one text
value in the Quantity column, as you can see in row 12, in the screen
shot below.
 If any values in a field are non-numeric, Excel uses the Count
function for the Value field, instead of the Sum function. 
Try This Pivot Table
Before you build your own pivot table in an Excel workbook, you can see
how a pivot table works, by trying the interactive Microsoft Excel example
shown below.

Below the interactive pivot table, there are a few things you can try, to see
different results in the pivot.

Note: You can download a copy of the file, to test the pivot table features
on your own computer.

Change the Filters


Above the data in the pivot table, there are filter fields - State, Business
Type (BusType), and Flood.

Make the following changes to the pivot table filter fields, to change what
the pivot table is showing:

 In cell B1, click the drop down arrow, to see a drop-down list of State
codes
o Uncheck one or more of the state code, to remove that data
from the pivot table display
o At the bottom of the drop down list, click the OK button
o The value in cell B1 changes from (All) to (Multiple Items)
o The pivot table shows the total amounts for the selected states
only, instead of all the states
 In cell B3, click the drop down arrow, to see a drop-down list with Y
and N, for Flood
o Select Y (yes), then click the OK button
o The value in cell B3 changes from (All) to Y
o The pivot table shows the total amounts for only the insurance
policies that have flood insurance coverage
Sort the Pivot Table Values
Currently, the pivot table is sorted alphabetically by the values in the Rows
area -- the Construction type, and the Region.

To see the data in a different way, you can follow these steps to sort by the
policy count

 In column A, select one of the cells with a region name, such as cell
A8 (East)
 In cell A6, with the Row Labels header, click the drop down arrow 
 Point to Region, then click Sort by Value 
 In the Sort by Value (Region) dialog box, click the arrow at the right
side of the "Select Value" box 
 Click on Count of Policy
 For Sort Options, click the radio button for Largest to Smallest 
 Click the OK button

After you click OK, in the pivot table, under each construction type, the
regions are listed from largest to smallest, based on the policy count field.
Understand the validating and auditing techniques in excel

Suppose we have the following formula in the D2 cell for


calculating interest for an FD account in a bank.

If we want to check the formula’s precedents, we can press F2 to


get into edit mode after selecting the required cell so that
precedents cells get bordered with various colors and written in the
same color and cell reference.

We can see that A2 is written with blue in the formula cell, and with
the same color, the A2 cell is bordered.

In the same way,

B2 cell has a red color.

C2 cell has a purple color.

This way is good. But we have a more convenient way to check


precedents for the formula cell.

We can use the “Trace Precedents” command in the “Formula


Auditing” group under the “Formulas” tab to trace precedents.
Select the formula cell and click on the “Trace
Precedents” command. Then, you can see an arrow, as shown
below.

We can see that precedent cells are highlighted with blue dots.

#2 – Remove Arrows
We can use the “Remove Arrows” command in the “Formula
Auditing” group under the “Formulas” tab to remove these
arrows.

#3 – Trace Dependents
This command traces the cell, dependent on the selected cell.

Let us use this command using an example.

Suppose we have four amounts that we can invest in. We want to


know how much interest we can earn if we invest.

In the above image, we can see that we have applied a formula for
calculating interest with Amount 1 and specified interest percentage
and duration in the year.

We will copy the formula and paste it into the adjacent cells for
Amount 2, Amount 3, and Amount 4. One can notice that we have
used an absolute cell reference for G2 and I2 cells as we do not
want to change these references while copying and pasting.

If we want to check which cells are dependent on the G2 cell, we will


use the “Trace Dependents” command available in the “Formula
Auditing” group under the “Formulas” tab.
Select the G2 cell and click on the “Trace Dependents” command.

In the above image, we can see the arrow lines where arrows
indicate which cells are dependent on the cells.

We will remove the arrow lines using the ‘Remove


Arrows’ command.

#4 – Show Formulas
We can use this command to display formulas written in the excel
sheet. The shortcut key for this command is ‘Ctrl+~.’

See the below image, where we can see the formulas in the cell.
We can see that we can see the formula instead of the formula
results. For amounts, the currency format is not visible.

Press ‘Ctrl+~’ again or click on the ‘Show Formulas’ command to


deactivate this mode.

#5 – Error Checking
This command is used to check the specified formula or function
error.

Let us take an example to understand this.

See the below image where we have an error in the function applied
for the result.

We will use the ‘Error Checking‘ command to solve this error.


The steps would be:
Select the cell where the formula or function is written, then
click “Error Checking.”

As we click on the command, we get the dialog box “Error


Checking.”

In the above dialog box, it can be seen that there is some invalid
name error. Therefore, the formula contains the unrecognized text.

Suppose we use the function or construct the formula for the first
time. In that case, we can click on the“Help on this Error” button,
which will open the help page for the function in the browser,
where we can see all the related information online, understand the
cause, and find all the possible solutions.

We will find the following page as we click on this button now.


On this page, we get to know about the error that this error occurs
when:

1. The formula refers to a name that has not been defined. For
example, the function name or named range has not been
described earlier.
2. The formula has a typo in the defined name. It means that
there is some typing error.

If we have used the function earlier and know about the function,
then we can click on the “Show Calculation Steps” button to check
how the evaluation of the function results in an error.

If we click on this button, the following steps are displayed:

 The next dialog box displays when we click the “Show


Calculation Steps” button.
 After clicking on the ‘Evaluate’ button, the underlined
expression, i.e., ‘IIF,’ gets evaluated and gives the following
information as displayed in the dialog box.

Does the above image show that the “IIF” expression is evaluated as
an error; #NAME? The following expression or reference, i.e., B2, was
underlined. If we click the “Step In” button, we can check the
internal details and come out by pressing the “Step Out” button.
Now, we will click on the “Evaluate” button to check the result of
the underlined expression. After clicking, we get the following
result.

 After clicking the “Evaluate” button, we get the result of the


applied function.
 We got an error. As a result, as we analyzed the function step
by step, we learned that there was some error in “IIF.” We can
use the “Insert Function” command in the “Function
Library” group under the “Formulas” tab.

As we typed the “IF,” we got a similar function in the list; we


needed to choose the appropriate function.
After selecting the “IF” function, we get the following dialog box
with text boxes for argument, and we need to fill in all the details.

After clicking on “OK,“ we get the result in the cell. Next, we will
copy down the function for all the students.
Things to Remember
1. The dates are also shown in the number format if we activate
the ‘Show Formulas’ command.
2. While evaluating the formula, we can also use “F9” as
a shortcut in Excel.
Different charting techniques in excel

Milestone Chart
A milestone chart allows you to plot milestones on a
timeline. This chart type can be useful when you’re planning
a new project and want to visually show the planned
milestones during a certain period (or chart the milestones
that have been achieved in the past).

A milestone chart visually shows you the milestones and the


distance between each milestone (as shown below).

When I was in my day job, we used to create a milestone


chart when we were planning a new project and had to
report interim updates and deliverables. We showed the
dates when we planned the check-in call and interim/final
deliverables.

While you can have this data in a boring table, plotting it as


a milestone chart helps visually see the progress (as well as
the time between milestones).
Below is a video where I show how to create a milestone
chart in Excel:

Gantt Chart
Gantt chart is quite popular with project managers.

It is used to for creating a schedule for a project or tracking


the progress.

With a Gantt chart, you can visually see:

 What all tasks/activities are schedules

 On what date a task starts and ends

 Number of days it takes for each task to get


completed

 Any overlap on a date with other activities.

Below is an example of a Gantt chart (which is about Alfred


– the butler – creating the Batsuit for Batman).
The biggest benefit of using a Gantt chart is that it shows
you if there are any days where multiple activities/tasks
overlap. This can help you plan better for your project.

Another good use of this kind of Gantt chart can be to plot


leaves taken by your team members. It will show you the
dates when more than one team member is on leave, and
you can plan ahead.

Bell Curve
A bell curve (also known as normal distribution curve) is a
way to plot and analyze data that looks like a bell curve.

It is often used during employee appraisals or in


schools/colleges to grade students.

While a bell curve is nothing but a ‘scatter line chart’ that


you can insert with a single click, the reason I have added it
as one of the advanced charts is that there is some pre-
work needed before creating this chart.

If you have the employee ratings data or student marks


data, you can not directly plot it as a bell curve.

You need to calculate the mean and the standard


deviation values and then use these to create the bell curve.

Sales Funnel Chart


In any sales process, there are stages. A typical sales stage
could look something as shown below:

Opportunity Identified –> Validated –> Qualified –> Proposal


–> Win

If you think about the numbers, you would realize that this
forms a sales funnel.
Many opportunities are ‘identified’, but only a part of it is in
the ‘Validated’ category, and even lesser ends up as a
potential lead.

In the end, there are only a handful of deals that are won.

If you try and visualize it, it would look something as shown


below:

If you have these numbers in Excel, then you can easily


create a sales funnel chart. If you’re using Excel 2016, you
have the option to insert the sales funnel chart directly
from the insert tab.

But for versions prior to Excel 2016, you’ll have to use some
charting trickery.

Below is the video where I show how to create a sales


funnel chart in Excel 2013 or prior versions:

Pareto Chart
Pareto Chart is based on the Pareto principle (also known as
the 80/20 rule), which is a well-known concept in project
management.

According to this principle, ~80% of the problems can be


attributed to about ~20% of the issues (or ~80% of your
results could be a direct outcome of ~20% of your efforts,
and so on..).

This type of chart is useful when you want to identify the


20% things that are causing 80% of the result. For example,
in a hotel, you can create a Pareto chart to check the 20%
of the issues that are leading to 80% of the customer
complaints.

Or if you’re a project manager, you can use this to identify


20% of the projects that are generating 80% of the revenue.

If you’re using Excel 2016, you can insert the Pareto chart
from the Insert tab, but if you’re using Excel 2013 or prior
versions, then you need to take a few additional steps.
Below is a video where I show how to create a Pareto chart
in Excel 2013 and prior versions:

Bullet Chart
Bullet chart is well suited for dashboards as it can punch a
lot of information and takes very little space.

Bullet charts were designed by the dashboard


expert Stephen Few, and since then it has been widely
accepted as one of the best charting representations where
you need to show performance against a target.

This single bar chart is power-packed with analysis. It has:

 Qualitative Bands: These bands help in identifying


the performance level. For example, 0-60% is Poor
performance (shown as a dark blue band), 60-75%
is Fair, 75-90% is Good and 90-100% is Excellent.
 Target Performance Marker: This shows the target
value. For example, here in the above case, 90% is the
target value.
 Actual Performance Marker: This column shows
the actual performance. In the above example, the
black column indicates that the performance is
good (based on its position in the qualitative
bands), but it doesn’t meet the target.

Below is a video on how to create a bullet chart in Excel

Step Chart
A step chart can be useful when you want to show the
changes that occur at irregular intervals. For example, price
rise in milk products, petrol, tax rate, interest rates, etc.

While Excel does not have an inbuilt feature to create a step


chart, it can easily be created by rearranging the data set.

Below is an example of a step chart.


Now if you’re thinking why not use a line chart instead, have
a look at the below charts.

Both of these charts look similar, but the line chart is a bit
misleading. It gives you the impression that the petrol prices
have gone up consistently during May 2015 and June 2015
(see image below). But if you look at the step chart, you’ll
notice that the price increase took place only on two
occasions.

Below is a video where I show how to create a step chart in


Excel.

Waffle Chart
A waffle chart is a pie chart alternative that is quite
commonly used in dashboards. It’s also called the squared
pie chart.

Related Article: Creating a Pie Chart in Excel

In terms of Excel charting, a Waffle chart doesn’t really use


any of the charting tools. It’s rather created using cells in
the worksheets and conditional formatting.
Nevertheless, it looks like a proper chart and you can use it
to jazz up your dashboards.

Below is an example of Waffle chart in Excel.

What do I like in a Waffle Chart?

 A waffle chart looks cool and can jazz up your


dashboard.

 It’s really simple to read and understand. In the


KPI waffle chart shown above, each chart has one
data point and a quick glance would tell you the
extent of the goal achieved per KPI.

 It grabs readers attention and can effectively be


used to highlight a specific metric/KPI.

 It doesn’t misrepresent or distort a data point


(which a pie chart is sometimes guilty of doing).

What are the shortcomings?


 In terms of value, it’s no more than a data point
(or a few data points). It’s almost equivalent to
having the value in a cell (without all the colors
and jazz).

 It takes some work to create it in Excel (not as


easy as a bar/column or a pie chart).

 You can try and use more than one data point per
waffle chart as shown below, but as soon as you
go beyond a couple of data points, it gets
confusing.

Thermometer Chart
A thermometer chart is another example where you can
show the performance against a target value (similar to
Actual vs Target chart or bullet chart).

It’s called a thermometer chart as you can make it look like


a thermometer.
Note: Thermometer chart can be used when you’re
measuring the performance of one KPI only. If you have to
analyze multiple KPIs or metrics, you need to either create
multiple thermometer charts or opt for a regular actual vs
target chart I covered earlier.

Below is a video where I show how to create a thermometer


chart in Excel.

Different formatting techniques in excel


Let's have a look at some easy instances of data formatting in Excel. Suppose we
have a simple sales report for a company that looks like this:
We need to format the data in this report because it is not attractive to viewers.

Now, in order to format data in Excel, we'll do the following things:

o The Font size is larger.


o We make the text of the column head bold
o Center aligning the data
o We will apply the outline border with the help of the shortcut that is
(Alt+H+B+T),
o After selecting the full table (using Ctrl+A), use the shortcut
key (Alt+H+O+I) to adjust the column width.
o To alter the background, use the 'Fill Color' command in the 'Font' group on
'Home'.

We will use a similar format for the 'Total' row, which is the last row of the table,
with the help of the 'Format Painter' command, present in the 'Clipboard' group on
the 'Home' tab.
Because the amount gathered is in a currency so, we have to format the same as
currency, using the command available in the 'Number' group which is placed on
the 'Home' tab.

After selecting the cells, that need to be formatted as currency, click the arrow above
to open the 'Format cells' dialogue box.

Select the 'Currency' option and click on 'OK.'


We can also apply the outline border to the table.

Now our next task is to generate a label for the report. With the help of
the Shapes, we will make the report's label. If we want to create the shape above
from the table, we'll need to add two new rows. To do so, we have to select the row
by 'Shift+Spacebar' and then press 'Ctrl+'+" twice to insert two rows.
Now we can insert the shape, to insert shape, go to the 'Shapes' command in
the 'Illustration' group of the 'Insert' tab and select an appropriate shape.

Create the shape that meets the requirement and add text to it by right-clicking on
the shape and selecting 'Edit Text.'
We may also utilize the 'Format' contextual tab to format the shape with commands
like 'Text Outline', 'Text Fill', 'Shape Fill', Shape Outline.' etc. We can also use the
excel formatting on text using the commands available in the 'Front' group, which is
placed on the 'Home' tab.
Conditional Formatting can also be used to draw the visitor's attention to the 'Top
3' and 'Bottom 3' salespeople. To perform this, we have to select the
option Top/Bottom rules in the conditional formatting and then we have to select
the option named Top 10 Items.

After that, the dialog box will open, and in this dialog box, we have to fill the number
for top ranks and the color we want.
Similarly, we can do this for Bottom 3.

We can also use other options of conditional formatting, such as Data Bars.
We can also generate the chart to display the data, which is also part of "Excel Data
Formatting.'

Shortcut Keys to Format Data in Excel


There are various shortcut keys to format data in Excel:

o Ctrl+2 or Ctrl+B, we can use this shortcut to make the text bold.
o Ctrl+3 or Ctrl+l if we want to make the text italic, then we can use this
shortcut.
o Ctrl+4 or Ctrl+U if we want to underline the text, then this shortcut is helpful.
o Alt+H, FA shortcut is used to open the 'Alignment' Dialog box.
o Alt+H, FN shortcut is used to open the 'Front' Dialog box.
o Alt+H, FK is used to make the font size of the text smaller.
o Alt+H, FG is used to make the font size of the text larger.
o Alt+H, B is used to add border.
o Alt+H, A then C is used to center align cell content.
o Ctrl+1 is used to open the 'Format Cell' Dialog box.
o Ctrl+Shift+Ampersand (&) is used to apply an outline border to the
selected cells.
o Ctrl+Shift+Percent (%) is used to apply the percentage format with no
decimal places.
o Shift+F8 is used to add a non-adjacent cell or range to a selection of cells.
o Ctrl+5 is used to apply or remove strikethrough Data Formatting Excel.
Cíeation of table , view and íepots in excel

Let’s intíoduce ouí dataset fiíst. ľhis is a souíce data table


that consists of 4 columns and 7 íows. Ouí goal is to cíeate a
íepoít as a pivot table fíom this souíce data table.

Step 1: Cíeate a ľable Using the


Pivotľable Featuíe
As we have alíeady known the benefits of pivot tables, follow
the steps below to cíeate a pivot table.

 Fiíst,
select the whole woíksheet that contains youí
souíce data table. ľhen, go to Insert > PivotTable. A
dialog box will pop up.
 In the Table/Range box, put the location of the souíce
dataset ( In this example, B4:E10 undeí Sheet1). ľhen
select the taíget location wheíe you wish to keep youí
pivot table. Afteí that, click on OK. Now, heíe aíe 2
cases,
Choosing New Worksheet will set a table in a new sheet.
Choosing an Existing Worksheet will set the table at a
paíticulaí location in the existing sheet. In the Location box,
put the location of the fiíst cell wheíe you wish to place youí
table.
A blank pivot table in the taíget location will be cíeated.

Read More: How to Make a Monthly Expense Report in Excel (with


Quick Steps)
Step 2: Manage the Layout of the Pivot ľable
ľhe Pivot Table Field List is located on the íight side of the
sheet and divided into the following two paíts.
ľhe Field Section includes the names of the fields that
coííespond to the column names of the souíce dataset.

ľhe Layout section includes the Repoít Filter, Row Labels,


Column Labels, and the Values aíea. You can modify the fields
of the table heíe.
Read More: How to Create an Expense Report in Excel (With Easy
Steps)

Similar Readings
 Make Daily Sales Report in Excel (with Quick Steps)
 How to Make Monthly Report in Excel (with Quick Steps)
 Create a Report That Displays Quarterly Sales in Excel (with
Easy Steps)
 How to Make MIS Report in Excel for Sales (with Easy Steps)
 Make Inventory Aging Report in Excel (Step by Step
Guidelines)

Step 3: Add or Remove a Field to Pivot Table


If you want to add a field to the Layout section, make suíe the
tick maík in the check box next to the field name. Similaíly, you
can íemove a field fíom a pivot table by unchecking the box
next to the field name.
Notes:
MS Excel includes the fields in the Layout section in the
following ways.
 Numeric fields aíe included in the Values aíea.
 Text fields aíe included in the Row Labels aíea.
 Date or time hieíaíchies aíe added to the
ColumnLabels aíea.

Read More: How to Create an Income and Expense Report in Excel


(3 Examples)

Step 4: Arrange a Pivot Table Fields


You can aííange a pivot table in the following ways.

 Drag and Drop fields among the fouí aíeas undeí the
Layout section. You can also alteí the oídeí of the fields
by díagging and díopping.
 Undeíthe Field section, íight-click on the field name, and
then click on the aíea wheíe you need to add it.

 Clickon the down aííow next to the field name to get a


díop- down list that includes all the available options
foí that specific field.
Basic of SQL and running queries
What are the SQL queries?
SQL stands for Structured Query Language. It is the language used by the databases to get the
information. We will learn how to query using the SQL language.

SQL Queries in SQL Server


The basis of a query in SQL Server is the SELECT sentence which allows to select the data to be
displayed. To start with this, we will use the AdventureWorks database that contains sample
tables and views which will allow us to have the same tables and data. We will also be able to
work with multiple tables already created.

 Note: For more information about installing the AdventureWorks database, refer to the
following article – Install and configure the AdventureWorks2016 sample database

SQL Queries and the Select Sentence

Let’s start with the SELECT sentence, the select sentence will allow us to get data from a table.

The following query will show all the columns from a table:

1SELECT *

2FROM [HumanResources].[Employee]

Try to use the SELECT statement in one line and the FROM statement in a different line. It is
easier to read that way. Select * means to show all the columns from a table. Another way to do
the same is the following example:

1SELECT [Employee].*
2FROM [HumanResources].[Employee]

The square brackets are optional. They could help if the column names have spaces (which is not
recommended). You can also select specific column names like this:

1SELECT [LoginID],[Gender]

2FROM [HumanResources].[Employee]

The previous example shows the loginid and gender column. As you can see, the data is
separated by commas. You can also use aliases to have a shorter name like this:

1SELECT e.Gender

2FROM [HumanResources].[Employee] e

The previous example uses the alias e for the table Employee. We can also use an alias for the
column names like this:

1SELECT e.Gender g

2FROM [HumanResources].[Employee] e

The column alias for Gender is now g. The next example will show the 2 different possible values
in the gender column (Male or Female):

1SELECT DISTINCT e.Gender g

2FROM [HumanResources].[Employee] e

Note that DISTINCT is a slow command and if the table has several millions of rows, it could take
time to execute and could bog down performance.

Another example is the TOP clause. This clause is used in SQL Server and not used in other
databases like Oracle or MySQL. The following example will show the first 10 rows of the table:

1SELECT TOP 10 e.[BusinessEntityID], e.Gender g

2FROM [HumanResources].[Employee] e

If we want to order data by a column, the order by is very useful. The following example will show
how to show the BusinessEntityID sorted in descending order.

1SELECT [BusinessEntityID]

2FROM [HumanResources].[Employee] e

3ORDER BY [BusinessEntityID] desc


 Note: For more information about select queries, please refer to this link:Learn SQL: SELECT
statement

SQL queries to filter data using the WHERE


command

The where command is one of the most common clauses used inside the SELECT command. This
clause allows filtering data. The following example shows how to check the BusinessEntityID of
the employees whose job title is Design Engineer.

1select [BusinessEntityID], [JobTitle]

2from [HumanResources].[Employee] e

3where JobTitle='Design Engineer'

 In the example, we used the equal operator. For a complete list of T-SQL operators, refer to
this link: Logical Operators (Transact-SQL)

Another powerful operator is the LIKE. Like, can help us in a search. The following example shows
the BusinessEntityID and the Job Title of the employees whose titles start with Design:

1SELECT [BusinessEntityID], [JobTitle]

2FROM [HumanResources].[Employee] e

3WHERE JobTitle LIKE 'Design%'

 For more information about the LIKE operator, refer to this link:SQL Like logical operator
introduction and overview

The IN operator is a very common operator also, the following example will show all the
employees whose JobTitle are equal to Engineering Manager or Senior Tool Designer:

1SELECT [BusinessEntityID],JobTitle
2FROM [HumanResources].[Employee] e

3WHERE JobTitle in ('Engineering Manager','Senior Tool Designer')

SQL Queries with aggregate functions and the


use or the group by statement

In the SQL queries, we need the SUM of the rows, the Average, and other aggregations functions.
These functions are often used with the group by and the having statements.

The first example will show the SUM and the average of the subtotal of the SalesOrderHeader
tables:

1SELECT SUM([SubTotal]) AS SUBTOTAL,AVG([SubTotal]) AS AVERAGESUBTOTAL

2FROM [Sales].[SalesOrderHeader]

The next example is showing how to get the sum of the orderQty column and the salesorderid
from the salesorderdetail table. We are grouping the information by salesorderid and ordering
the sum in descendant order:

1SELECT SUM([OrderQty]) AS Qty, [SalesOrderID]

2FROM [Sales].[SalesOrderDetail]

3GROUP BY [SalesOrderID]

4ORDER BY SUM([OrderQty]) DESC

 For a list of aggregate functions, refer to this link:Aggregate Functions (Transact-SQL)

SQL Queries to get data from multiple tables


One of the most important features of the tables is that you can query multiple tables in a single
query. To do that we use the JOINS. There are several types of JOINS. INNER JOIN, OUTER JOIN,
LEFT JOIN, RIGHT JOIN. The different types of joins allow to JOIN tables in a different way.

 We created a special article for these types of joins. For more information, refer to the
following link: SQL multiple joins for beginners with examples
Creation of table , view and reports in SQL
Creating Views
Database views are created using the CREATE VIEW statement. Views can be
created from a single table, multiple tables or another view.

To create a view, a user must have the appropriate system privilege according to
the specific implementation.
The basic CREATE VIEW syntax is as follows −
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in a similar way as you
use them in a normal SQL SELECT query.

Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example to create a view from the CUSTOMERS table. This view
would be used to have customer name and age from the CUSTOMERS table.

SQL > CREATE VIEW CUSTOMERS_VIEW AS

SELECT name, age

FROM CUSTOMERS;
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual
table. Following is an example for the same.

SQL > SELECT * FROM CUSTOMERS_VIEW;


This would produce the following result.
+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+
The WITH CHECK OPTION
The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of
the WITH CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the
condition(s) in the view definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following code block has an example of creating same view
CUSTOMERS_VIEW with the WITH CHECK OPTION.

CREATE VIEW CUSTOMERS_VIEW AS

SELECT name, age

FROM CUSTOMERS

WHERE age IS NOT NULL

WITH CHECK OPTION;


The WITH CHECK OPTION in this case should deny the entry of any NULL values in
the view's AGE column, because the view is defined by data that does not have a
NULL value in the AGE column.

Updating a View
A view can be updated under certain conditions which are given below −
 The SELECT clause may not contain the keyword DISTINCT.
 The SELECT clause may not contain summary functions.
 The SELECT clause may not contain set functions.
 The SELECT clause may not contain set operators.
 The SELECT clause may not contain an ORDER BY clause.
 The FROM clause may not contain multiple tables.
 The WHERE clause may not contain subqueries.
 The query may not contain GROUP BY or HAVING.
 Calculated columns may not be updated.
 All NOT NULL columns from the base table must be included in the
view in order for the INSERT query to function.
So, if a view satisfies all the above-mentioned rules then you can update that view.
The following code block has an example to update the age of Ramesh.

SQL > UPDATE CUSTOMERS_VIEW

SET AGE = 35

WHERE name = 'Ramesh';


This would ultimately update the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT
statement would produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Inserting Rows into a View
Rows of data can be inserted into a view. The same rules that apply to the UPDATE
command also apply to the INSERT command.

Here, we cannot insert rows in the CUSTOMERS_VIEW because we have not


included all the NOT NULL columns in this view, otherwise you can insert rows in a
view in a similar way as you insert them in a table.

Deleting Rows into a View


Rows of data can be deleted from a view. The same rules that apply to the
UPDATE and INSERT commands apply to the DELETE command.

Following is an example to delete a record having AGE = 22.

SQL > DELETE FROM CUSTOMERS_VIEW

WHERE age = 22;


This would ultimately delete a row from the base table CUSTOMERS and the same
would reflect in the view itself. Now, try to query the base table and the SELECT
statement would produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |

You might also like