100% found this document useful (1 vote)
378 views127 pages

Plex SQL Queries B099751BWX

Uploaded by

moon26

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
378 views127 pages

Plex SQL Queries B099751BWX

Uploaded by

moon26

Copyright:

© All Rights Reserved

Available Formats

Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 127

SQL

Build Complex SQL Queries


© Copyright 2021 - All rights reserved.
The contents of this book may not be reproduced, duplicated or transmitted without direct written
permission from the author.
Under no circumstances will any legal responsibility or blame be held against the publisher for any
reparation, damages, or monetary loss due to the information herein, either directly or indirectly.
Legal Notice:
This book is copyright protected. This is only for personal use. You cannot amend, distribute, sell, use,
quote or paraphrase any part or the content within this book without the consent of the author.
Disclaimer Notice:
Please note the information contained within this document is for educational and entertainment
purposes only. Every attempt has been made to provide accurate, up to date and reliable complete
information. No warranties of any kind are expressed or implied. Readers acknowledge that the author
is not engaging in the rendering of legal, financial, medical or professional advice. The content of this
book has been derived from various sources. Please consult a licensed professional before attempting
any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances is the author responsible for
any losses, direct or indirect, which are incurred as a result of the use of information contained within
this document, including, but not limited to, —errors, omissions, or inaccuracies.
Table of Contents
Introduction
Chapter One: Introduction to SQL
What Is SQL?
How Does It Work with Databases?
Benefits of Working with SQL
Chapter Two: Basic SQL Commands
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Query Language (DQL)
Data Control Language
Data Administration Commands
Transactional Control Commands
Chapter Three: Data Types You Can Use Inside of SQL
Characters Fixed in Length
Variable Characters
Numeric Values
Literal Strings
Boolean Values
Chapter Four: An Introduction to Numeric Data Types
Absolute Numerics
Approximate Numerics
Operators in SQL
Chapter Five: An Introduction to Character Strings
Character Data Type
Character Varying Data Type
Character Large Object (CLOBs) Data Type
National Character Large Object Data Type, National Character and
National Character Varying
Booleans
Chapter Six: An Introduction to Datetimes
Time without Time Zone Data Type
Timestamp with Time Zone Data Type
Date Data Type
Chapter Seven: An Introduction to Intervals
Row Type
REF Types
Collection Types
User-Defined Types
Chapter Eight: Manage the Objects in Your Database
The Schema
Creating a New Table
Exercise
Chapter Nine: Retrieving Data
Retrieving Data
Creating Views
Adding Data
Adding One Row at a Time
Deleting Data
Solutions
Chapter Ten: How to Work with Search Results Through SQL
Creating Your New Query
Using the SELECT Command
Working with Case Sensitivity
Chapter Eleven: An Introduction to Joins
How to Use A Join
Basic Types of Join
Examples of SQL Join Types
What Do You Do Next?
Chapter Twelve: An Introduction to Subqueries
Why Should You Use A Subquery?
What Do Subqueries Do?
Chapter Thirteen: An Introduction to Set Operators
UNION and UNION ALL
INTERSECT
MINUS
Chapter Fourteen: Rookie Mistakes
Assuming Your Clients Know What They Need
Isolating Database Table Designs
Ignoring the Scope of the Project
Considering Technical Factors Alone
Using only one Development Environment
Skipping Testing
No Documentation
Never taking feedback
Chapter Fifteen: Retrieving Information From Databases
Verify the Structure of the Database
Test the Database
Always Check Queries with Subselects
Summarize Data using GROUP BY
Double Check Queries with JOIN
Understand Clause Restrictions
Handle Error Conditions with Care
Always Use Parentheses for Logical Operators
Control the Retrieval Privileges
Create a Regular Backup
Chapter Sixteen: Using SQL With Applications
SQL In an Application
SQL Embedded Java Program
SQL Embedded C Program
SQL Embedded FORTRAN Program
SQL Embedded COBOL Program
Chapter Seventeen: Database Security
Delegating Responsibility
Referential Integrity
Chapter Eighteen: SQL Injections
Chapter Nineteen: Exercise
Solutions
Chapter Twenty: Case Study
Using INNER JOIN to Identify the StatusName
Combining Reason Table Rows Using UNION
Using a Subquery to Produce the Final Result that Includes the Derived
Table
Final Query
Conclusion
References
Introduction
Over the last few years, a lot of data has been collected and continues to flow
in huge amounts every day. One of the most important things for a business is
analyzing and viewing the data correctly to gain insights and get the right
outputs. It is also important to ensure you use the data correctly to make the
right decisions. Numerous programming languages have been developed
which can be used to analyze this information. Some examples are C, which
is a popular server development and operating system for embedded systems.
If you want to use the databases correctly, you need to use languages
developed to assess the information in the database. One such language is the
Structured Query Language (SQL), first introduced in the 1970s.
SQL is a language that can be used to create, generate, manage and
manipulate data in relational databases. Businesses often prefer the use of
relational databases that can store hundreds and thousands of rows of data,
but this can only happen when the database is well designed. SQL is the only
database language that can be used to manage large databases. Therefore, it
makes sense to learn how to work with and manage data with SQL.
Throughout this book, you will gather information about SQL and the
importance of learning the language. You will also learn some basic
commands used in SQL and explains how you should use the relevant
commands to manipulate and use the necessary information in the databases
and tables. This book has the necessary information about different types of
operators, data types, queries and functions that can be used to work with the
data and analyze the information to obtain the relevant outputs. This book
also has many examples you can use for practice and some exercises that will
help you practice these concepts and understand them better.
Continue to use the information in the book and practice the examples to
master SQL. It is also important to know which code you should use and
when to use it to obtain the relevant information. Only when you practice will
you know where you should apply a specific operator or function.
Thank you for purchasing the book. I hope you get the information you need
to master SQL.
Chapter One: Introduction to SQL
You can choose from multiple options when it comes to learning new
programming languages, some of which can be used to build your website.
You can also use the website to reach your customers in a way that makes it
easier for you to convert any potential customers into confirmed customers.
Other languages allow you to develop applications and games for your
phone. Some programming languages are slightly advanced, which makes
them harder to use if you are an amateur.
Most companies use the Database Management System (DBMS) to hold the
relevant information that the company can use. This information also
includes details and their products and services, and customers. DBMS was
one of the first options that came onto the market, making it easier to work
with the data. Since the first version of SQL came out, many iterations were
developed to help businesses. The latest versions hold on to their information
without any difficulty. The simplest DBMS systems designed now are more
secure than those developed a few years ago.
Every company needs to hold enough information about:

1. The company
2. Employees
3. Customers
4. Products and services, and more.
It is important to manage this information safely and securely. To do this, the
database needs to be secure. There should also be a way for people to connect
the information to analyze the data. It was then that the relational database
management system was developed, a more secure database that uses
technology to ensure that the information is safe.
If you are a business owner, you need to look at your options and choose the
right tools to manage the database. SQL is the perfect tool for this purpose
since it is well designed and easy to use. This language was designed to
manage data in databases and data warehouses. It also has the necessary tools
and functions which can help you keep the data secure. This book will look at
what SQL is and how you can use it to protect data.
What Is SQL?
SQL or Structured Query Language is a programming language that is easy
to learn and use. It enables users to interact with different types of data and
tables in the data warehouses or databases. These tables are always on the
same system. SQL was developed in the 1970s, but it gained popularity after
IBM built a prototype like SQL and launched it. Most businesses started
using this model to analyze their businesses and make some judgments about
their business. Oracle was the language that IBM developed, and many
businesses are still using this tool all over the world. Both SQL and Oracle
are used by businesses to keep the information secure. It is also important to
determine if the data is available for the database owner to use. These
languages make it easier for one to perform analysis because of the many
tools that they provide the user with.
How Does It Work with Databases?
If you choose to work with an SQL database, you will also look at the
relevant groups of information while you look at the database. Some people
consider these to be the organizational mechanisms that store information that
your user can get ahold of later on, efficiently and effectively. You can do
this while helping businesses obtain the right information without dealing
with bugs and errors.
Relational Databases
Most businesses use relational databases since it allows them to separate the
data into various data types and logical units. These tables are interconnected
in the database, allowing you to make sense of the data depending on the type
of information you require. It is a good tool to use based on the information
you need. Use the database to take the complex information and break it
down into smaller pieces of information. This will make it easier to manage
and optimize the data depending on how you need it.
Bear in mind that you need to use the right databases and queries to extract
the relevant information from the database. These databases make it easier for
you to use the data since you can break complex information into a language
that anybody can understand. You can use the server to look at the relevant
information in the data set and see how you can make the query and
information work together. You can also locate the information you are
looking for quickly since you do not have to go through the entire database.
Add some security to your data if you want to keep it safe from others. This
is of utmost importance.
Server and Client Technology
In the beginning, the computers that were used for most companies were
mainframe computers. This means that the computers were used to support
large systems that were ideal for storing information and processing it. The
user could go to these computers and interact with the mainframe, commonly
known as a dumb terminal or one that does not interact on its own. If you
wanted to get the information to bring up the right function, the dumb
terminals would need to rely on all the information, such as the storage,
processor, and memory found inside the computer.
Most computer systems function differently because each has different
characteristics in terms of hardware and resources. The client/server system
relies on these resources and, with a different process, helps you obtain the
results you need. With this one, the main computer, called the server, will be
accessible to the user on your network. The user needs to have the right
information. You can obtain this information from more than one server or
even from a desktop computer if you need to. The user is known as the client,
and both server and client easily interact with the database.
Working with Internet or Cloud-Based Systems
For most businesses, server or client technology is often popular. It has
worked well for many companies, but some different things are needed at
times because of changes in technology. Some companies need to have good
databases in their systems so that users can access the data online. Customers
who have an account with a company would be using this kind of database
system whenever they wanted to change the information or update
something. Customers can also make purchases, pay online and even cancel
their purchases if needed.
More companies are now moving to the cloud or online since it is easier to
work with Internet-based database systems. At times, you may want to add
passwords and other security information for when customers are adding
personal information, such as their credit card information. Most companies
now require their customers to use the relevant information to do this. Most
customers may not be willing to provide this information, especially if they
must give it for free.
It is easy for one to work on the system and handle it. Having said that, there
are some situations when you need to work behind the scenes to ensure the
relevant information appears on the screen. A customer only needs to log into
their system, and there is a lot of stuff that the server will have to piece
together to make the information show up properly on the screen. It is also
important for you to develop the right query to display the relevant
information.
For instance, you can decide if the web browser you want to use or develop
will use a SQL query or program. This is the only way for you to determine if
any user can extract the relevant information. The SQL system will be used to
reach your database as soon as the customer inputs what they are looking for.
You can use an SQL query to bring this information onto the website. This is
the best way to determine if the system works properly. It is important to note
if you keyed in the right information to extract the output.
Benefits of Working with SQL
We have now looked at different database systems you can work with. Let us
now look at some benefits of the SQL database or language. There are
numerous database languages out there, and other coding languages, and all
of them have different benefits of using them. It is important to know these
benefits to have the necessary information to motivate you to learn SQL.
High speed
If you wish to work with information quickly, it is best to use the SQL
database system. You will be surprised at how much information this holds
and how quickly the SQL system can find what you need. This is an efficient
method that makes it easier for you to work with the database.
Well-Defined Standards
Any business that uses SQL will have the necessary security standards to
ensure the database is strong and works well. Some other types of databases
may not have the relevant information, making it difficult to store some of
the necessary information.
No Need to Code
Most people believe they need to learn to code if they must work with an
SQL statement or database. You can use some codes to make this easier. We
will look at some of these in further detail later in the book. You do not have
to be a master of a language to start using this.
Earn More Money
When you work for an organization that uses SQL, you earn more because of
your expertise. Nurture your programming skills in SQL or learn how to
maintain a system and keep it running effectively and efficiently. You can
also work as an SQL analyst and provide information and insights for a
business that will help the seniors make better decisions. These skills can
maximize any business’s profits.
Most Technology Uses SQL
Most businesses use various databases, such as MySQL, Microsoft SQL
Server, PostgreSQL, and SQL itself, to perform different functions. Bear in
mind that people who use SQL at work need to master it since the data is
often stored in a SQL database.
Employers Look for SQL Skills
Most employers want their employees to know how to use SQL. Yes, an
employer is willing to pay you more, but they also are aware of the benefits
of hiring an individual who is skilled at using SQL. If you do not want to
move jobs or want to switch to a better role, you should learn to code in SQL.
Bear in mind that this skill set is something every employer wants.
There is an Answer in the Database
Think about all the questions you have about the data set. You need to gather
more information about the sales made during the year. Were your customers
satisfied with your product? Have your expenses reduced since last year? The
database will have the answers, and you can use SQL queries to extract this
information. When you identify the database that provides this information,
you can use SQL to explore that data to answer your questions. SQL allows
you to analyze data in different ways. There is no need to rely on the reports
generated by your team or even contact them to extract the relevant
information. You would only need to use SQL to extract this information.
Object-oriented DBMS
The SQL database will use the DBMS system to help make it easier to find
the information you want, store items, and do so much more.
The File Size Does Not Limit You
Have you had issues with using a spreadsheet? Did the file you prepared
crash because of the large volumes of data in it? If you want to avoid
something like this from happening, you should use a relational database that
allows you to store the required information. SQL allows you to perform
different operations on the data and use that information to make decisions.
Excel is a good tool to use, but it is not equipped to perform the required
operations on millions of rows of information. Try to use a relational
database to store the information and use SQL to perform any type of
analysis on the data.
Easy to Generate Reports
It is easy to write an SQL query and re-use it when you need to. It is also easy
to make the necessary changes to the query when you need to, depending on
the need. You can also use comments in the SQL code that make it easier for
you and anybody else to understand the query. If you only use spreadsheets,
you need to include multi-step processes. The first step is to import the
relevant information into the data set from a report. You should then tabulate
and sort it. Once you add the filters to the data set, you can use the relevant
values based on your requirement.
If you want to use SQL, all you have to do is write the code only once. You
can save it and then re-open it when you produce the report. This will save
you a lot of time.
There are some benefits you can enjoy when you use SQL. When you work
on SQL, you may take some time to get used to the interface. It is a little
difficult to use, and some of the features may be difficult to understand since
they come from a third party and not from SQL. There are some good
features, ease of use, and so much more that make working with SQL code
one of the best options to help your business out.
SQL is an invaluable tool that most employers want to use. Since most of the
information maintained is digital, there is a lot of data available to you. This
information is either stored in the data warehouse or database. If you want to
manage these databases or warehouses, you need to learn SQL. If you read
any business journal, you will see they are looking for someone with
experience in analysis. If an organization wants to do more with its data, it
should have individuals who know how to access and analyze data. It is easy
to do this using SQL.
Chapter Two: Basic SQL Commands
We now know a little about SQL and some commands you can use when you
work with SQL. It is time to learn a few of the commands you need to use to
get this system to work. Luckily, SQL is easy to learn. There are different
types of commands you can work with. In this chapter, we will spend some
time learning these commands and separating them into the six different
categories that are the best for them. These six categories include:
Data Definition Language (DDL)
SQL is a DDL, and this means you can generate or work with new objects in
the database. It is one of the aspects inside your SQL program that allows you
to generate objects into the database before arranging the objects in the way
you need to use them. For instance, this is the aspect of the system that you
will use when you want to make changes, such as adding or deleting objects,
in the table. There are a few different commands that you would be able to
use for this, including:
● Drop index
● Drop view
● Create index
● Alter index
● Alter table
● Drop table
● Create table
Data Manipulation Language (DML)
SQL is often termed a DML since you can manipulate different parts of the
data using SQL queries and commands. This is also the one you will use
when you would like to do some modifications to the objects inside the
database. It is best to use this method if you want to perform the following
functions:

1. You want to update the object


2. Delete an object
3. Insert the relevant information in the database
This will give the new user the freedom to make the necessary changes to the
dataset. You can also add something new that enhances or improves the
database.
Data Query Language (DQL)
When you work with a DQL, you work with one of the most important parts
of a SQL database. A DQL can perform different activities on the database
itself, which is considered modern. There is just one command needed to
work with the DQL part, and this command is the "Select" command. You
can use this command in various ways, including using it to run queries
inside a relational database. If you are keen on getting detailed information
from the dataset, use the SELECT command. You can perform the necessary
function easily.
Data Control Language
Data Control Language (DCL) is a component in SQL that is important to
learn to use. You need to use these commands to determine who has access to
the data in your database or data warehouse. If you are dealing with personal
information like credit card information, it is good to have some limitations
on who can access the system and get the information. DCL commands that
you can use to generate the objects are related to the different types of
information found in the database. This also includes the information you
need to use. The following are some commands which you can use:
● Grand
● Alter password
● Revoke
Data Administration Commands
When it comes to the commands you can perform within a SQL database, it
is important to analyze or audit these commands and operations performed
within the database. There are some instances where you can access the
database’s performance by using some of these commands. If you want to fix
anything in the database that causes issues on the system, you need to find a
way to eliminate these bugs. You can use the following commands to help
you identify where there are bugs or errors. There are different commands
you can use to perform these activities, but the popular commands include the
following:
● Stop audit
● Start audit
Another thing you need to remember when working with SQL is that
database administration and data administration are different from each other.
These ideas are independent of each other inside the system. For instance,
database administration is the part that will manage all your databases,
including the different commands you are setting up in SQL. It is also
important to be specific when it comes to implementing the SQL database in
your system.
Transactional Control Commands
It is important to manage and track the transactions you perform in the
database. You need to track these commands to know what activities are
being performed in the data set, especially the transactional commands. If
you run a company that sells products online, you need to track the
transactions performed on the website. This will maintain a record of all the
transactions in the database. There are several things that you can use these
transactional control commands for, including:
Savepoint
Using this command, you can generate various points within the transactions
made. You can also use this command along with the Rollback command to
differentiate between the relevant information.
Commit
The commit command is used to save the necessary information related to
different transactions you make within the database.
Rollback
Using this command, you can look through the entire database and reverse
any transactions you make within the database.
Set Transaction
This command is often used when you try to take any transaction in your
database and give it the right name. You will often use this one whenever you
are trying to label things for a bit more organization.
These data types are important since your results depend on them. Use the
right data type depending on what you want to get out of the search. We will
look at these types in greater detail in this book and give you examples of
how you can use them. You will also learn how to divide up the information
correctly to avoid issues. This is the only way you can manage and maintain
your databases. You can use different techniques to organize the database
using different operations and queries in SQL.
Chapter Three: Data Types You Can Use Inside of
SQL
We will now look at the various data types you can work with or use in SQL,
especially when creating or writing a new code. The data types will vary with
every SQL implementation you are working on. It is also important to know
how to use these data types correctly so you are doing the right thing with the
data set. Only when you know the use of these data types can you determine
how best you can use them to improve the working of the database. The data
types that are mostly found inside of SQL will be the attributes that go with
the information present in the dataset. These specific characteristics are
placed on a table so that you can retrieve and read them easily.
A classic example of this is to use the right data type if you want the column
you are working on to only store integers. You can use SQL to set it up so
that the user cannot place anything outside of a number within the database,
or at least in that cell of the table. If you want customers to add personal
information, such as their phone number or credit card, ensure you use the
right data types to ensure people do not accidentally put something else there.
When you assign the right kind of data to the different fields inside the
database, you ensure fewer errors in the entry of data on the customer's side.
Another thing you need to remember is these data types will change slightly
depending on the SQL version you are working with. You must check out
your SQL version’s rules to make sure that it is all in order. In most cases,
use the data points specific to your version so that the database is set up
better.
Different data types supported by SQL depend on the history of the tool you
use. There are five predefined general types:
• Numerics
• Boolean
• Datetimes
• Strings
• Intervals
You can have more than one subtype within each of these types, and we will
look at this in further detail later in the book. In addition to the built-in,
predefined types, the SQL version released in 2019 supports collection types,
constructed types, and user-defined types. It also supports the use of
multidimensional arrays, and the use of operators on those arrays.
If you are not using the SQL 2019 implementation, you may need to ensure
you use the right data types since some legacy SQL systems may not work
with undescribed data types. Before you choose to create and use different
data types, you must ensure the DBMS you use allows you to port to other
types, as well.
We will now spend some time learning more about how to look at different
data types.
Characters Fixed in Length
The first data type we will look at is the fixed-length character. If you want to
work with constant characters or even strings that do not change, ensure they
are saved properly. This means you should save them as a fixed-length data
type. The typical data type that you will use when we are working with these
options is:
Variable Characters
You can also work with variable characters in the SQL database. Do not limit
the user and choose the length. This one works well for things like names
(which can be varying lengths) as well as passwords and usernames to make
them more unique.
CHARACTER VARYING (n)
If you were to use this command or data type, the number “n” would signify
the maximum length of the value you fill in the database. You can pick from
several different types to use when you want to work with these variable
characters, including VARCHAR, ANSI, and VARCHAR2. If you wish to
use this data type, you do not have to limit things when you fill the spaces the
user can work with. If you say n = 15, the user can add up to 15 characters in
the field.
If you want to work with any character string considered a variable, you must
ensure you use the right data type. You should not vary too much from the
values you have stated in the code. This helps you maximize the amount of
space inside your database, and you can ensure that the user can put in the
right information without a lot of issues in the process.
CHARACTER (n)
If you use this data type in your SQL query, write it within parentheses. This
is the only way to ensure you do not have any errors in your code. For
instance, this could be the phone number of the customer. You should also
ensure you do not add any details or information to a field that is longer than
the chosen number of characters.
There are some variations on how you do this. Let us say that for the name,
you will set the length as 20. If you have customers with small names, you
can use them in this query. Having said that, you cannot go over the 20-
character limit if you do have a customer whose name is longer.
Some SQL implementations also use the CHAR data type to save the relevant
information in the database or dataset. It is good to work with this kind of
data type when you would like to work with alphanumeric information. For
instance, it is important to set up the part in the database where you want the
details of customers to be stored. You can then set the character limit to be
two parts so that everyone knows how to put things in.
If you work with this data type, your user will add too much information to
the data set or form. Let us assume you have customers who live in South
Dakota. If you set the database up, ensure you have the right number of
characters against the field to enter the relevant information.
There are many places where you can limit the number of characters that you
would like to use, but when it comes to the username and password that the
user picks, you should not use the fixed-length data types. The user will need
to make up the username and password credentials that work for them, and
some people will pick a much longer one to make it safe.
Numeric Values
You may also work with different numeric values when you work with SQL.
These values are the ones that are stored in the fields in the data sets in the
number format. This would indicate values of different types couldn’t be
stored in the column. These values will go by a different name, basically
depending on the type you are working with. The following are some
numerical data types you will be working with:
• DECIMAL(p, s)
• REAL(s)
• BIT(n)
• FLOAT(p)
• INTEGER
• DOUBLE PRECISION(P)
• BIT VARYING(n)
Literal Strings
You will also work with a data type called literal strings. A literal string has a
series of characters. These include different information, such as names and
phone numbers, and the database or program specifies these. For the most
part, you will find that these strings have a lot of data that may match where
there are similar characteristics.
When you work with literal strings, you may have some trouble specifying
the data type you will use. You should specify the string type you want to use
to ensure you have the right output. It is good to note that when you work on
these kinds of strings, especially alphanumeric kinds, you will need to add
some quotes around the words. Either use single or double-quotes. Ensure
you do not use different types of quotes across the code.
Boolean Values
The Boolean value is very important since it helps you work with various
SQL programs and applications with ease. When you work with these kinds
of values, there will be three different values that you can work with: null,
false, or true. You will also find that the usage of Boolean values improves
the way you work with different units of data. It will take the information that
you are working on and compare; you will get the answers above. For
instance, if you work in SQL, specify the search parameters, and all the
conditions that come back will either be null, false, or true, depending on
what you are trying to compare.
When you work with Boolean values, you will obtain the relevant
information. This is only going to work if the commands you use meet the
condition. If this answer is false or null, the data will not be retrieved for the
customer or you. If you do not have the right values, you will see that the
information is inaccurate. A good example of this is when the user searches
your database. If the keywords in the product match with the information you
are looking for, these are the answers that will show up.
As an online store, if you use the Boolean value, you will improve your
application. It will ensure that you can get the results that you or the user
needs when searching. This is the only way to ensure you have the right
products appearing on your customers’ screens. You can also use the Boolean
expression to ensure you have the right setup in the system and that the items
in the database match the information you need. Do not worry if this sounds
complex; the SQL system will help you get this done in a simple manner so
that your customers find the things they want without having to worry or
fight with the website.
You can use different applications where the Boolean operator is used, but as
a beginner, you may not want to use this method often. It is best to use this
command only when you are certain about the information you are working
with. You can also use it when you want to look through your database and
find specific answers to questions. Alternatively, you can use it when you
look through the program or store to find the relevant information, which you
can then add to your SQL tables to speed up the process.
Throughout this chapter, you learned about different data types that you can
work with within the SQL database. This is the only way you can make
searching in the database easy. Each data type will work differently so that
you can get the right results and more that you need.
Apart from the above commands, several others can be used in SQL. These
queries and other commands can only be used for specific reasons, so you
cannot use these words as part of any other variable name. If you were to
give the names of keywords or reserved words to columns, variables or
tables, you are going to have errors in your code. Just think how SQL would
react if you were to key in the following command:
SELECT SELECT FROM SELECT WHERE SELECT = WHERE;
Chapter Four: An Introduction to Numeric Data
Types
Absolute Numerics
I am sure the name would have given away what one can do with absolute
numeric types. You can obtain the exact value of any number entered in the
database. The following are the data types under this category:
• Numeric
• Integer
• Bigint
• Smallint
• Decimal
Numeric Data Type
You can include both an integer and fractional component when you use the
NUMERIC data type. This data type allows you to specify the number of
characters you want to declare for any NUMERIC variable. There are two
aspects to consider:

Precision: The total number of digits


Scale: The number of digits in the fractional portion of the number
If you have the NUMERIC data type included in your code, then the SQL
implementation you use will give you the necessary precision and scale that
you need. Alternatively, you can specify the precision and scale you need by
initializing or declaring the variable along with the scale and precision. You
can enter the precision and scale in the following format: NUMERIC (p, s),
where p stands for precision and s stands for scale.
Let us assume the default precision of this data type in any SQL
implementation you use is 12, and the default scale is 6. If you added a
column with the NUMERIC data type in the database, you could enter values
up to 999999.999999. If you specify the data type to be NUMERIC (10) for
any column, it is an indication that the column can only hold values up to
9999.999999. The parameter can specify the number of digits that you or a
user can enter in a column. If you do specify the data type as NUMERIC (10,
2), it indicates the largest value you can enter in the column is 99999999.99.
The NUMERIC data type can include values like 595.72. Let us understand
how this value can be deciphered – since there are five digits, the precision of
this variable is 5. The scale is 2, which is the number of digits after the
decimal point. It is best to use the data type if you have decimal values in
your data set.
Integer Data Type
If you use the INTEGER data type, there is no fractional part that can be
used. The precision of this is based on the implementation or version of SQL
you use. A database developer cannot specify the precision. This precision is
based on the number of digits that can be present in every INTEGER
variable.
Smallint Data Type
The SMALLINT data type can be used for integers, but the precision of this
data type cannot be any larger than the precision of an INTEGER on the same
implementation. Implementations on any computer often represent
SMALLINT and INTEGER with 16-bit and 32-bit binary numbers,
respectively. In most implementations, SMALLINT and INTEGER are the
same. When you define the database table column to hold integer
information, you know there is a range of values in the column that will not
exceed the precision of SMALLINT data on your implementation. In this
case, assign the column the SMALLINT type rather than the INTEGER type.
You can use this assignment to conserve storage space.
Bigint Data Type
The BIGINT data type came with the 2003 version of SQL, and it can be
used extensively in the SQL 2019 implementation. It is also an integer type,
and it is defined as a type whose precision is at least as great as that of the
INTEGER type and could be greater. The precision of this data type is based
on the implementation method used.
Decimal Data Type
This data type has the same properties as the numeric data type, but it can
include a fractional component if required. This indicates that you can specify
the scale and precision of the value. The difference between the two data
types is that the precision of any query you write will be greater when you
use a decimal data type instead of the latter. The use of this data type uses
default values, like a NUMERIC data type, but in the latter, you do not
specify the precision or scale.
Let us assume you specify an item NUMERIC (5, 2). This data type does not
contain any numeric value which is above 999.99. If you initialized the data
type DECIMAL (5, 2), you could enter any values greater than 999.99.
If your data includes any fractional position, use the DECIMAL and
NUMERIC data types. You can also use the INTEGER, SMALLINT, or
BIGINT types if the data includes whole numbers. You can also use the
NUMERIC type to increase the portability of the data you store in the data
set. For instance, if you were to say NUMERIC (5, 2), you can move the data
from one hardware to another without worrying about the values in the
system.
Approximate Numerics
Some variables you add to the database may have large value ranges.
Usually, in such cases, exactness isn’t necessary, and a close approximation
is acceptable. Three approximate numeric data types can be used to work
with this kind of data.
Float Data Type
If you are keen on moving the database from one hardware base to another,
this is the data type you should use. Every hardware server stores some
information in the registers, and most platforms have different sizes of these
registers. These registers will be different from the ones that were initially
used. When you use the FLOAT data type, you can specify how precise you
want the value to be. For instance, you can say FLOAT (5). If the hardware
you use supports this precision value, then it is an indication that your system
uses single-precision arithmetic. If this precision value needs arithmetic
precision, you need to use double-precision arithmetic.
Experts recommend you use the FLOAT data type instead of other data types,
such as REAL and DOUBLE PRECISION, if you want to move between
hardware systems with ease. The data type gives you a chance to specify the
precision. The FLOAT data type does not depend on the hardware, but the
other depends on the hardware.
If you do not know which data type you should choose or which numeric data
type you need to use, pick absolute numeric types. These numeric types do
not expect the system to use more resources to perform functions. This is the
only way you can get the approximate result. If these output values are within
any range, you need to use an approximate data type.
Real Data Type
The REAL data type gives you the single-precision floating-point number.
The precision of this data type is dependent on the implementation. The
hardware you are using determines precision. For instance, a 64-bit machine
gives you more precision than a 32-bit machine.
The floating-point number is one where the number has a decimal point that
appears or floats in various locations of the number. This is dependent on the
number’s value. Some examples of floating-type numbers are 3.1, 3.14, and
3.14159.
Operators in SQL
Use different operators in SQL to perform different mathematical operations
on the operands in the data set. These operators are addition (+), subtraction
(-), multiplication (*), and division (/). It is also easy to use the first two
operations to perform any function on dates.
Syntax
The syntax is as follows:
SELECT <Expression>[arithmetic operator]<expression>...
FROM [table_name]
WHERE [expression];
The expression is made of a single scalar function, column name, variable, or
constant in the above syntax. You can also use a piece of any SQL query to
compare these values against each other.
Chapter Five: An Introduction to Character Strings
Every database stores different types of data types, including sounds,
animations, and graphic images. The data types you often use are numeric
and character string types.
There are three types of character data sets:

1. Fixed character data (CHARACTER or CHAR)


2. Varying character data (CHARACTER VARYING or
VARCHAR)
3. Character large object data (CHARACTER LARGE OBJECT or
CLOB)
There are other variants of these types of character data: NATIONAL
CHARACTER, NATIONAL CHARACTER VARYING, and NATIONAL
CHARACTER LARGE OBJECT.
Character Data Type
If you use this data type to define the nature of the column, you can also
specify the number of characters that the column can hold. You can use the
syntax CHARACTER (x), where x is the number of characters. If you specify
the data type for any column as CHARACTER (16), it indicates the
maximum length of any data you can enter in the column is 16 characters. If
you do not specify this argument, then there is no maximum length. SQL
assumes a field length of one character. If you enter data into a
CHARACTER field of a specified length and you enter fewer characters than
the specified number, the remaining character spaces will be filled by SQL.
Character Varying Data Type
This data type is used only if there are entries in the column that can vary in
length. Do not expect SQL to pad or fill up the fields in your data set. There
are going to be blank. Using this data type allows you to store the required
number of characters that the user enters. No default value exists for this data
type. To specify this data type, use the form CHARACTER VARYING (x)
or VARCHAR (x), where x denotes the number of characters permitted when
using this character.
Character Large Object (CLOBs) Data Type
This data type was introduced when the SQL 1999 version was released. As
its name implies, this data type is used if you have any huge character strings.
This data type is only used when the strings cannot be keyed in using the
CHARACTER type. CLOBs behave much like ordinary character strings, but
there are several restrictions on what you can do with them. A CLOB may
not be used in a PRIMARY KEY, FOREIGN KEY, or UNIQUE predicate.
Further, you may not use these data types to compare other data types. Since
CLOBs have a large size, applications generally do not transfer CLOBs to or
from a database. A special client-side type known as a CLOB locator is used
to manipulate the necessary information in the database. This parameter uses
a value that helps you identify large character objects.
National Character Large Object Data Type, National
Character and National Character Varying
Every programming language has characters, and each of these is different
from the characters defined in another programming language. This is true
even for spoken languages. For example, Spanish has tildes and other accents
over alphabets unlike the English language character set. Russian has a
different set of characters compared to characters in English. When you
specify the set of characters in English as the default in your system, you can
use an alternate set. The National Character Large Object Data Type,
National Character and National Character Varying function like the
character data types we looked at in the previous section. The only difference
between these sets of data types is that these sets are different from the
previous sets. You can either specify the table or column or use the character
set you want to use in the code. Each column can use a different character set.
In this example, we will look at how you can create tables using multiple
character sets.
CREATE TABLE XLATE
(
LANGUAGE_1 CHARACTER (40),
LANGUAGE_2 CHARACTER VARYING (40) CHARACTER SET
GREEK,
LANGUAGE_3 NATIONAL CHARACTER (40),
LANGUAGE_4 CHARACTER (40)
CHARACTER SET KANJI
);
The characters in the default of the implementation are present in the
LANGUAGE_1 column. In the above code, we see that the national character
set is used in the LANGUAGE_3 column. The second column,
LANGUAGE_2, has Greek information. The last column only contains kanji
characters.
Booleans
This data type uses conditions and only gives the output as TRUE or FALSE.
It may also give you an unknown output depending on the condition used. If
either a TRUE or FALSE value is given as the output, then the condition
works fine. If the output is unknown or NULL, then the result is based on an
unknown value.
Chapter Six: An Introduction to Datetimes
Three data types deal with the dates and times in the system you are working
on. These are termed as datetime data types. Since there is a lot of overlap
between these data types, the implementation or use of these data types
varies. Some versions of SQL may not support the use of these types. In such
situations, you can switch between one or more of these data types.
Implementations that do not support all three data types for dates and times
may lead to some problems with working on the databases. If you migrate
from one implementation to another, you will need to see how to use these
data types. If you have trouble with migration, check the representation of
dates and times in both the source and the destination implementations to
represent dates and times before using the function.
Time without Time Zone Data Type
This DATETIME data type stores time values, including second, hours and
minutes. The first two only take whole numbers as their value, while the
seconds can be single, double, or fractional values. If you have this data type
in your code, you can enter the time as 09:32:58.436. The parameter will read
this as 9 AM, 32 minutes and 58.436 seconds.
The fractional part in the output is precise and is dependent on the
implementation of the output. The numbers take up six positions, while the
colons in the output take up two extra positions. Therefore, when you write
the function in SQL, ensure you have enough space for eight positions. This
only happens if the value you enter does not have a fractional part. It also
happens if all nine positions are not used. When you write the function as a
query, you specify it as TIME. Using this value, you can obtain the number
of default fractional digits. You can also define the precision or digits in the
variable by adding the precision parameter.
Timestamp with Time Zone Data Type
This data type is used in the same way as the previous data type. The only
difference between the two types is this data type offsets any information
from the universal time before it gives the output. This extra information in
the output takes a few more positions and more memory in the computer.
There will be six extra digits, which succeeds the output without the time
zone data type. The output of this command has 25 positions in the memory,
and there is no fractional part in this output. There are 26 positions, including
the number of fractional digits for fields that include a fractional part. This
means the first six digits in the output are the fractional digits.
Date Data Type
This data type is used to store the month, year, and day values. The length of
each of these values is two, four, and two, respectively. A DATE value can
be used to represent any date between 0001 and 9999. The length of a DATE
is ten positions, as in 1957-08-14. Since SQL represents the four digits in a
year, the DATE data type gives an output with all four digits in the year. The
SQL data types were never affected by the change in the year format,
especially the Y2K problem.
Chapter Seven: An Introduction to Intervals
The datetime and interval data types are related to each other. As the name
suggests and interval data type relies on the difference between the values of
two dates. In most applications that use times, dates or both, it is important to
define the interval between these two times or dates. In most SQL
implementations, there are two distinct intervals – the daytime intervals and
year-month intervals. If you want to look at the difference in the months and
years between two dates, you can use the year-month interval. A daytime
interval data type will give you the number of seconds, minutes, hours, and
days between two dates in a month. It is important for you never to mix
calculations that use the year-month interval with any calculation that
involves a daytime interval because every month has a different set of days.
Row Type
This data type was first introduced when the SQL 1999 version was
developed. It can be difficult to understand and use this data type, and not
many people use it. Another important fact to note about this data type is that
it does not adhere to the normalization rules initially defined with the
relational database theory. Another defining characteristic is you that can’t
have a multivalue in the field table row. A field may contain only one value.
Using this data type, you can declare the data that is present in each row in
the table. This indicates there is one row, which is present in another row.
In the example below, we look at how you can define a row type to add a
person’s address:
CREATE ROW TYPE addr_typ
(
Street CHARACTER VARYING (25)
City CHARACTER VARYING (20)
State CHARACTER (2)
PostalCode CHARACTER VARYING (9)
);
Now use the data types below to create a new row type which can define the
entire table.
CREATE TABLE CUSTOMER (
CustID INTEGER PRIMARY KEY,
LastName CHARACTER VARYING (25),
FirstName CHARACTER VARYING (20),
Address addr_typ Phone
CHARACTER VARYING (15)
);
From the above code, you can easily define the advantage. You can maintain
information for different entities in one place. You can include the addresses
of customers, employees, stockholders, and vendors in one place. The only
thing you need to do when you define the ROW type is to also declare the
variable you want to use against that data type.
REF Types
A REF data type is not an important part of core SQL. They are, however,
used by many expert programmers. This data type is not a distinct data type,
using both the NUMERIC and CHARACTER data types. Instead, it is a
pointer to a data item, row type, or abstract data type residing in a row of a
table (a site). If you dereference the pointer, you can retrieve the information
stored in the target site of the database. If you are confused, do not worry
because you are not alone. If you want to use a REF type, it requires you to
know a little bit about object-oriented programming (OOP). You need to
understand the principles of OOP. Since the REF data type is not a core part
of SQL, you do not have to learn more about how to use it.
Collection Types
When SQL moved out of the relational database form of querying with the
1999 implementation of SQL, the data types that first violated the
normalization principles could now be used. Programmers could use one data
type or field to contain collections of objects instead of only one object. It
was only in the SQL 1999 implementation that the ARRAY data type was
defined. The MULTISET data type only came with the SQL 2003
implementation, and it can be used extensively in the latest SQL 2019
implementation.
You can compare two collections with each other if you want to, but this can
only happen when they use the same data type since both data types use the
same form or type. Since arrays have a defined element order, you can also
compare the elements within arrays. This means you can compare one value
with the next in an array. A multiset does not have a defined order of
elements, but it can easily be compared if the values are enumerated. It is
only then that you can compare the values in the multiset. It becomes easy for
you to compare any paired enumerations.
Multiset Type
A multiset is an unordered collection. Specific elements of the multiset may
not be referenced because they are not assigned a specific ordinal position in
the multiset.
Array Type
An array data type does not work in the same way as other data types. It
violates the first normalization rule, also known as the first normal form, but
differently compared to the ROW data type. The ARRAY type is a collection
of variables, and these variables do not necessarily have to be distinct, unlike
a CHARACTER or NUMERIC data type. This means arrays allow one data
type to be within the data set. For instance, if your organization must connect
with your customers regardless of where they are - at home, work, or while
traveling. To do this, you need to maintain a record of all their numbers. Let
us understand this using the following example. In the lines of code below,
we will declare the phone attribute as an array variable.
CREATE TABLE CUSTOMER
(
CustID INTEGER PRIMARY KEY,
LastName CHARACTER VARYING (25),
FirstName CHARACTER VARYING (20),
Address addr_typ
Phone CHARACTER VARYING (15) ARRAY [3]
);
Using the ARRAY [3] notation, you can store between one and three
telephone numbers in the CUSTOMER table created above. The telephone
numbers stored are the best example of a repeating group. If you have a
classic relational database, you cannot use a repeating group in the database.
This example is from the SQL 1999 implementation where the rules of a
relational database were bypassed. The functional flexibility using which the
data integrity was managed in SQL was thrown out the window when Dr.
Codd first listed the normalization rules in the SQL 1999 implementation.
This, however, increased the complexity of the database structure, which
translated into issues with data integrity. These issues only arose when users
were unaware of the effects of various actions on the database. Bear in mind
that every array is only ordered in a way where the values are given ordinal
positions. These elements are not associated with each other.
User-Defined Types
This is another feature which came into the picture in the SQL 1999
implementation. This implementation focused on object oriented
programming, and gave a SQL programmer access to all the data types used
in the later versions of SQL. In the SQL 2019 version, you can use different
data types and perform a variety of operations on these data types. Like
abstract data types follow certain principles, so do user defined types. You
can create them using the same principles.

Distinct Data Types


A distinct data type is a simple form of the user-defined data type. A distinct
data type has a defining feature that is expressed in the form of one data type.
This data type can be developed using any predefined data type in the SQL
list of data types. This data type is called the source data type. You can create
multiple distinct data types in the code you write, and each of these data types
will rely on one source data type. These are different from each other and
cannot be compared. For instance, a distinct data type can be used to
differentiate between two currencies. Let us look at the example below:
CREATE DISTINCT TYPE USdollar AS DECIMAL (9,2) ;
Using the above code, you can create a US dollar currency type based on the
DECIMAL data type, which has been predefined. You can also create
another distinct data type using the following:
CREATE DISTINCT TYPE Euro AS DECIMAL (9,2) ;
If you want to use these data types, you can create a table for them.
CREATE TABLE USInvoice (
InvID INTEGER PRIMARY KEY,
CustID INTEGER,
EmpID INTEGER,
TotalSale USdollar,
Tax USdollar,
Shipping USdollar,
GrandTotal USdollar
);
CREATE TABLE EuroInvoice
(
InvID INTEGER PRIMARY KEY,
CustID INTEGER,
EmpID INTEGER,
TotalSale Euro,
Tax Euro,
Shipping Euro,
GrandTotal Euro
);
In the example above, the Euro data type and US dollar data type are both
new attributes, and these use the DECIMAL data type. You should not
compare one instance with another in the output. You can easily convert
currencies in SQL but would need to use the CAST operation. Once you
make this conversion, you can compare the values with each other, if needed.
Structured Types
Another form of a UDT is the structured data type. The constructor expresses
this data type as an attribute list with the definitions and methods used. These
data types are not based on one type of source data type.
Constructors
The database management system creates a constructor when you write or
initialize structured data types. This constructor and data type will use the
same name. This constructor can be used to data types and their values, so
you can use queries to map the values back to the default.
Mutator and Observer
The database automatically creates or generates two functions, namely the
mutator and observer functions, when a structured UDT is created in the
function. When you invoke a mutator, it can be used to change the value in
the attribute in the data type you have created. An observer function behaves
very differently compared to a mutator function in the sense that it retrieves
the value of any attribute present in the structured attribute. observer
functions can be used as part of SELECT statements if you want to obtain
values from the database. These functions do not change the value, and
therefore, can be used to extract just the values.
Supertypes and Subtypes
There can be a hierarchical or inheritance relationship between two or more
structured data types. For instance, there is one data type and a subtype. The
data type in this instance is AudiocCDudt and its subtype is VideoCDudt. Let
us also assume there is another subtype named DVDCDudt. The first data
type in this list is the super data type, while the others are subtypes.
VideoCDudt can be considered the proper subtype of the supertype if no
other supertype is present in the code that VideoCDudt relies on. If
VideoCDudt has a subtype named MP4CDudt, MP4CDudt is also a subtype
of AudioCDudt, but this will not be considered the exact or direct subtype of
AudioCDudt. It is important to understand this if you want to develop
applications with different codes.
Chapter Eight: Manage the Objects in Your
Database
books far, we have looked at the different data types you can use when you
work with databases. We have also looked at some commands you can use
and work with. Some examples in the book also have used simple queries,
and we will look at more of them across the book. We are now ready to start
learning some of the steps you need to work with when managing the objects
in your database. We will look at some of the different objects over the
course of the next few chapters, including synonyms, sequences, clusters,
tables, views, and tables. We will look at some examples and see how we can
work with different objects in the database.
The Schema
If you work with schemas on the database, you should look for a way to use
the different objects in the database. You also need to ensure you link the
schema only to one object. Do not let all the users access the schema. If only
one user has access to it, then he will be linked to the administrator account
in the schema. For example, anyone, especially the user, can generate the
object, and when they do this, they can generate their own schemas. This will
give you more control over the objects in the database, such as the ones that
you can change, delete, generate, or manipulate.
This is interesting for people who want to use this to make some changes to
the account. For instance, we will look at when a new customer is trying to
set up an account for your store. Most people tend to sign up for this,
especially because they can use their own username and password. If you are
the administrator of the system, you can use the schema easily. Once the
account is set up, they will be able to make some changes as is needed,
including changing their address, deciding on a new payment option, and
even making changes to the items they are ordering. If there is any situation
where you need to use the account, enter the username and password. You
can also mess around or play with the account.
Let us understand this better using the following example. Let us assume you
created the table and schema. You have the relevant information about the
table, as well. You also have the necessary information to log in using the
schema. In this example, we are going to use the PERSON1 username to
understand the access better. You can decide what you would like to place
inside this database, and you can even create a brand-new table. For this one,
we are going to call it EMPLOYEES_TBL. If you go back to the records to
look for the necessary information, you will notice that the new table will be
termed PERSON1 EMPLOYEES_TBL. This is how others will see the table
name, so they know who created the table. The schema will be the same for
everybody who worked on developing the table.
When you like to access a schema that you already own or use, you do not
have to use the name of the schema to perform any operations. The only thing
you would need to do is to call its name. So, for the example we went
through earlier, all you have to do is call the EMPLOYEES_TBL. If you
want to access this schema from a different area in the database, you should
include the name of the user who created the schema, too.
Creating a New Table
When you create a new table or schema in the database, you should ensure
you work on the table that stores the information you need. Creating some of
these tables is easy, and you can add in the information whenever you need
to. When you are ready to begin using a new table, use the command
CREATE TABLE. You can add the relevant information to the database and
ensure it has all the right information. You can also structure the table in a
way to make it look nice. Ensure you use the right information in the table.
Before you create this new table, consider what you would like to get done
with this table. You need to determine the following:

1. How big should the table be?


2. The data you want to include in the table
3. How would the organization use the information in the table?
Almost all SQL types you use will have characters you can use if you would
like to terminate or submit a statement to the server. One such character is a
semicolon that is best to use if you are using ORACLE to run the SQL query.
The Transact-SQL version allows you to use the GO command. So basically,
when you are ready to get started on a table, just type in CREATE TABLE
and then fill it all out, and you are ready to go.
Creating a new table with one that already exists
You may want to use the information from one table in another one. You may
also want to create a new table to do this. It is possible to do this when you
use SQL programming by using the right commands to make this happen.
Commands needed include SELECT and CREATE TABLE. When you
master the use of these commands in your code, you will notice the table you
created is like the old one. This is a feature to which you can make some
customizations so that you can pick what information will go from one table
to another.
Use the following commands if you want to take one of your tables and use it
to create a brand-new table.
CREATE TABLE NEW_TABLE_NAME AS
SELECT [ “|COLUMN1, COLUMN2] 
Example 1
Let us now construct or build a table that stores all the information about the
weather. You should ensure that you have no repetition in the data. Try
writing the code before you look at the next set of statements. This will help
you determine if you used the right statements or not.
CREATE TABLE STATION
(ID INTEGER PRIMARY KEY,
CITY CHAR(20),
STATE CHAR(2),
LAT_N REAL,
LONG_W REAL);
Example 2
Another table called ‘Station’ is present in the database already. We will add
three rows to this table, and this information will contain names of stations in
the United States.
INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112);
INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105);
INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68);
Exercise
Now that you have an idea about how you can insert information into a table
let us work on a small exercise. This will help you understand the different
functions explained so far.
Create a database that contains the information for a computer firm. The
schema in the database should have the following tables:

1. Product (Maker, Model, Type)


2. PC (Code, model, speed, RAM, HD, CD, Price)
3. Printer (Code, Model, Color, Type, Price)
4. Laptop (Code, Model, Speed, RAM, HD, Screen, Price)
The first table should have information about the various products and their
maker, model number, and product type. The model numbers in this table
will need to be unique to every product in the schema. The computers are
identified by unique codes and characterized by their model, RAM capacity,
processor speed, CD ROM speed, price, and hard disk capacity. The laptop
table is like the PC table except that we use the screen size instead of the CD
ROM Speed. Every model you key into the printer table has the output type
of printing technology, such as matrix, laser, or jet, and the color.
Chapter Nine: Retrieving Data
When it comes to working with the database, you need to understand the data
provided to you and not the structure of the database. To do this, you need to
do the following:

1. Add the data to tables


2. Retrieve the information
3. Display and change the information
4. Delete it from tables
It is very easy to manipulate the information in the database. You can add
information to the database or the table in a group. You can also add this
information row by row. Deleting, altering, or retrieving tables are easy tasks
to perform. The important thing to do is to select the relevant rows to change,
delete, or retrieve. The data you want to work with or analyze may reside in a
database, which contains a large number of variables. Fortunately, if you can
specify what you want by using an SQL SELECT statement, the computer
does all the searching for you.
Retrieving Data
This is a data manipulation task that most users frequently perform to retrieve
the selected information from any database. You may want to retrieve the
contents of one row out of thousands in a table. If you want to retrieve the
rows from the database that satisfy specific conditions or combinations, you
need to use the right operators along with the SELECT statement. You can
also retrieve the rows in the entire dataset. The SELECT statement can be
used to perform all these functions.
The simplest and easiest use of this statement is to retrieve the necessary
information in the table. To do so, use the following syntax:
SELECT * FROM CUSTOMER ;
The statement also has a wildcard character, which means you select all the
information in the table. In this context, the asterisk is a shorthand substitute
for listing all the column names of the CUSTOMER table. As a result of this
statement, you will have the relevant data in the columns and rows that
appear on your screen.
A SELECT statement can also be used in complicated statements. Some
SELECT statements are complicated, and it can be difficult to decipher them.
Several modifying clauses can be tacked into a basic statement which gives
rise to the potential complexity mentioned earlier. In this chapter, we will
discuss the use of the WHERE clause, which is the method used often to
restrict the rows that a SELECT statement returns.
A SELECT statement with a WHERE clause has the following general form:
SELECT column_list FROM table_name
WHERE condition ;
This column lists specific information that you want to display. It also
displays only the relevant information in the list. The FROM clause specifies
from which table you want to display columns. You can use the WHERE
clause to exclude any rows which do not satisfy the condition specified in the
query. The condition may be simple (for example, WHERE
CUSTOMER_STATE = ‘NH’), or it may be compound (for example,
WHERE CUSTOMER_STATE=’NH’ AND STATUS=’Active’).
The following example shows a compound condition inside a SELECT
statement:
SELECT FirstName, LastName, Phone FROM CUSTOMER
WHERE State = ‘NH’
AND Status = ‘Active’ ;
This statement is used to return the name and numbers of the active
customers who live in New Hampshire. The AND keyword means that row
must meet both conditions to qualify for retrieval: State = ‘NH’ and Status =
‘Active.’
Example 1
In this example, we will retrieve the ID, City, and State of the stations in the
North of the United States.
SELECT ID, CITY, STATE FROM STATION
WHERE LAT_N > 39.7;
The output will be
ID CITY STATE
44 Chicago ILL
66 Michigan MI
Example 2
In this example, we will retrieve the information of the stations that are in the
North.
SELECT * FROM STATION
WHERE LAT_N > 39.7;
The output will be
ID CITY STATE LAT_N LONG_W
44 Chicago ILL 40 105
66 Michigan MI 47 68
Example 3
Let us retrieve the information in the Table Stations.
SELECT * FROM STATION;
The output will be
ID CITY STATE LAT_N LONG_W
13 Chicago ILL 33 112
44 Michigan MI 40 105
66 Caribou ME 47 68
Example 4
In this example, we will retrieve only the ID, City, and State.
SELECT ID, CITY, STATE FROM STATION;
The output will be
ID CITY STATE
13 Chicago ILL
44 Michigan MI
66 Caribou ME
Creating Views
The structure of the database that is designed according to sound principles
increases the data integrity. This structure, however, is often not the best way
to look at the data. Numerous applications can use the same data, but every
application will have use the information collected differently. Another
powerful and important feature of the data set is its capability to display
different data set views. These views are different from the actual table and
do not represent how the table stores the information. You have base tables in
every view you use, and these tables are those which contain the columns and
rows you want to use to generate the view. This section looks at views in the
context of retrieving and manipulating data.
The SELECT statement returns virtual tables that contain all the information
we need. Views are a special virtual table, and telling the difference between
views and other virtual tables is done by looking at the metadata stored in the
database. In fact, that is the only way to hold the view’s definition. The
distinction between the two provides the view with a certain amount of
persistence that other virtual tables don’t have. The view can also be
manipulated in much the same way as a standard table with one difference –
the view’s data does not exist independently. The view’s data is derived from
the table/s where were the view columns come from, and each application
will have its own method to view data.
Consider a sample database that has the following tables:

1. Client
2. Tests
3. Employee
4. Orders
5. Results
Let us assume the national marketing manager wants you to give him
information about where the maximum number of orders come from. Some
of this information is present in the CLIENT table, while the other lies in the
ORDERS table. Let us assume you have a quality control office that wants
you to compare the information, which updated you about the final result.
This comparison requires some data from the ORDERS table and some from
the RESULTS table. If you want to satisfy this requirement, you need to
create views to give you exactly the data you want in each case.
Adding Data
When you create a table in a database, it will be empty. Even if you create a
table using the relevant tools, this table will only have empty fields. It will
follow a structure. If you want to put the table to use, you will have to add
data to it. The data you have may not be stored in the right form.
• If the data is not in the acceptable form for the database, you’ll need to
enter all the information manually. You can add value to the table
when you need to. Alternatively, you can use voice recognition
software or an optical scanner. However, most people don’t use these
devices to enter data.
• If your data is in the digital format but does not follow the relevant data
type, then you will need first to convert the data into the right format
before you enter any value into the database
• If your data is in the right format and is digital, you can transfer the
information to a new database
Depending on the data and its form, you should transfer all the information
directly to the database using one operation itself. To do this, you need to
enter every record only one row at a time. Bear in mind that every record you
enter in the database will correspond to only one row in the database table.
Adding One Row at a Time
Most databases support the use of form-based data entry. This feature makes
it easier for you to create screen forms for every column in your database.
Field labels will give you a chance to develop forms to enter the necessary
information on the database. You will, however, only enter one row at a time.
Once the database accepts the new row, the DBMS automatically clears the
form so that the user can enter a new value. This method enables you to add
data to a table one row at a time.
It is easy to use form-based data entry. This form of data entry is less
susceptible to errors when compared to working with comma-delimited
values. The only trouble with form-based data entry is that the process is
nonstandard. Every DBMS has its own methods of creating forms. A data
entry operator can deal with this problem. Forms can generally look the
same, and they may look the same even across different DBMS platforms.
The application developer is the person who would need to move back to the
basics and understand how to make changes to any development tool.
Another problem with form-based data entry is some implementations do not
permit all the validity checks on every value you enter.
The safest way to maintain this level of data integrity is to ensure you do not
have any bad data in your database. You can prevent the use of bad data by
applying the relevant constraints to the fields in the data entry form. This
approach allows you to ensure only the correct type of data is entered into the
database. These data types should fall within the predefined range. Applying
such constraints cannot prevent all possible errors, but it does catch some of
them.
Before you do this, check the validity of every variable and its type in the
code to ensure data integrity. To do this, you need to build your own screen.
It is also important to accept any data entries into any variables you declare in
the code using any application program code. Once you do this, you can enter
the valid entries into the data set.
Using the INSERT command, you can enter one row in the database at a
time. You can do this using the syntax below:
INSERT INTO table_1 [(column_1, column_2, ..., column_n)]
VALUES (value_1, value_2, ..., value_n) ;
The square brackets indicate that listing is optional. A default column in this
table relates to the sequence of columns in your main table. When you add
relevant values in the same sequence in the field or column, the elements will
move to the required columns. This will happen regardless of whether you
specify these columns or not. If you want to specify the values in a different
sequence, list the names of the columns before you enter the values. To do
this, put the columns in an order that corresponds to the order of the
VALUES.
If you want to enter any record or value into the CUSTOMER table, use the
syntax below:
INSERT INTO CUSTOMER
(
CustomerID, FirstName, LastName, Street, City, State, Zip Code,
Phone
)
VALUES
(:vcustid, ‘David’, ‘Taylor’, ‘235 Nutley Ave.’, ‘Nutley’, ‘NJ’,
‘07110’, ‘(201) 555-1963’
);
The first value in the report, vcustid, is the variable you increment using your
program code. You increase the value every time you enter a new row in the
table. Using this approach, you can ensure there are no duplications made to
the CustomerID primary key. This is done since a primary key must be
unique. The other items in the data are only values and not variables that
contain values. You can also hold the relevant information in the database if
you need to. The INSERT statement works equally well either with variables
or with an explicit copy of the data itself as arguments of the VALUES
keyword.
Deleting Data
Information is often rendered useless as time passes, and it is important to
remove this information from a database to ensure incorrect details are not
present in the database. Any irrelevant information in a table or database will
only slow the performance of the database since it consumes memory. This is
only going to confuse people who use the database. If you want to transfer
old information to an archived table and then move this database offline, do it
before the data changes. In this way, it becomes an unlikely event that you
may ever need to use the data again. You can also recover the necessary
information from the archived data. This also means the information is not
slowing down every time. Before determining if the data you want to use is to
be archived or not, you must know how to delete the information on the
database layer. The DELETE statement is SQL is best used for this purpose.
You can use the statement to delete rows, columns, or specific information
from the database.
Using this information, you can also delete every row in the database. The
easiest way to do this is by using the DELETE command or statement. Using
the WHERE clause, you can restrict the use of the DELETE statement only to
specific rows in the database. The syntax is like that of a SELECT statement.
The only difference is that you do not use any specifications in any column.
If you delete a table row, it indicates you want to remove all the information
present in that row.
For instance, let us assume David Taylor is your customer and he relocated to
Germany. This means he cannot buy anything else from you. This indicates
he can be removed from your database. To do this, use the following
commands:
DELETE FROM CUSTOMER
WHERE FirstName = ‘David’ AND LastName = ‘Taylor’ ;
If you only have one customer in the data set named David Taylor, you can
delete the relevant row using the above statement. If for some reason, you
have more than one user with the name David Taylor, you need to use the
WHERE clause to add the relevant differentiators, so only the correct user is
removed from the database.
Exercise
Create a table that stores normalized precipitation and temperature
information. Use the following information to create your table:

1. The ID field should match the ID in Table Station


2. You need to enforce the range for the other values in the table
3. Do not duplicate the Month and ID combinations
4. The temperature should be Fahrenheit
5. Rainfall should be measured in inches
Once you have created the table, perform the following operations:

1. Populate information for the months of January and July


2. Retrieve all the information in the table
3. Join the two tables
4. Retrieve information about the stations ordering the information
by month and greatest rainfall
5. Retrieve information about the temperature in July
6. Query to show the maximum and minimum temperatures
7. Query to convert Fahrenheit to Celsius using a new view
8. Query to show stations with an average temperature greater than
50 degrees
9. Retrieve the data from the new view
Solutions

1.
CREATE TABLE STATS
(ID INTEGER REFERENCES STATION(ID),
MOTH INTEGER CHECK (MONTH BETWEEN 01 AND 012),
TEMP_F REAL CHECK (TEMP_F BETWEEN -90 AND 120),
RAIN_I REAL CHECK (RAIN_I BETWEEN 0 AND 95),
PRIMARY KEY (ID, MONTH));

2.
INSERT INTO STATS VALUES (14, 2, 63.4, 0.26);
INSERT INTO STATS VALUES (24, 5, 92.7, 3.40);
INSERT INTO STATS VALUES (55, 2, 23.3, 0.93);
INSERT INTO STATS VALUES (55, 5, 77.8, 3.23);
INSERT INTO STATS VALUES (49, 2, 63.7, 1.13);
INSERT INTO STATS VALUES (56, 5, 54.8, 3.02);

3.
SELECT * FROM STATS;
The output will be
ID MONTH TEMP_F RAIN_I
14 2 63.4 0.26
24 5 92.7 3.40
55 2 23.3 0.93
55 5 77.8 3.23
49 2 63.7 1.13
56 5 54.8 3.02

4.
SELECT * FROM STATION, STATS
WHERE STATION.ID = STATS.ID;
The output will be
ID CITY ST LAT_N LONG_W ID MONTH TEMP_F RAIN_I
15 Arizona AZ 45 112 14 2 63.4 0.26
15 Arizona AZ 45 112 24 5 92.7 3.40
42 Colorado CO 53 105 55 2 23.3 0.93
42 Colorado CO 53 105 55 5 77.8 3.23
63 Michigan ME 57 68 49 2 63.7 1.13
63 Michigan ME 57 68 56 5 54.8 3.02

5.
SELECT MONTH, ID, RAIN_I, TEMP_F
FROM STATS
ORDER BY MONTH, RAIN_I DESC;
The output will be
MONTH ID RAIN_I TEMP_F
2 15 0.26 63.4
5 15 3.40 92.7
2 42 0.93 23.3
5 42 3.23 77.8
2 63 1.13 63.7
5 63 3.02 54.8

6.
SELECT LAT_N, CITY, TEMP_F
FROM STATS, STATION
WHERE MONTH = 7
AND STATS.ID = STATION.ID
ORDER BY TEMP_F;
The output will be
LAT_N CITY TEMP_F
47 Chicago 65.8
40 Michigan 74.8
33 Caribou 91.7

7.
SELECT * FROM STATION
WHERE 50 < (SELECT AVG(TEMP_F) FROM STATS
WHERE STATION.ID = STATS.ID);
The output will be
ID CITY ST LAT_N LONG_W
13 Chicago AZ 33 112
44 Michigan CO 40 105
8.
CREATE VIEW METRIC_STATS (ID, MONTH, TEMP_C,
RAIN_C) AS
SELECT ID,
MONTH,
(TEMP_F - 32) * 5 /9,
RAIN_I * 0.3937
FROM STATS;

9.
SELECT * FROM METRIC_STATS;
The output will be
ID MONTH TEMP_C RAIN_C
15 2 63.4 0.26
15 5 92.7 3.40
42 2 23.3 0.93
42 5 77.8 3.23
63 2 63.7 1.13
63 5 54.8 3.02
Chapter Ten: How to Work with Search Results
Through SQL
After you create a brand-new database for your business, you may need to
use the database to search to find the information you would like to use. Use
the SQL language to find any of the results you would like, but you need to
make sure that the database is set up correctly for your searches to find your
information. There are times when people will get onto a website you created
to look for a product you are selling. Are you more interested in setting up a
database to bring back the wrong results and is slow? Or do you want to
create an easy-to-use database and bring back the best results? This chapter
covers some information about the different queries you can use to obtain the
database's relevant information.
Creating Your New Query
When you start writing a new query, you will send the relevant information
to the database to obtain the necessary output. Bring up the right command,
the SELECT command, so you can do the query you would like to use. One
example is that of looking at the necessary table that holds all the relevant
products within it. Use the SELECT command to find all the products that
are in the table. The user, or yourself, can type in the ones that they want to
find, such as the best-selling items, ones that are a certain brand, and so on.
Use any kind of query that you would like to find the right product from the
database.
Using the SELECT Command
When you feel like creating or developing a query within the database, you
need to use the SELECT command to do this. This command will basically
take over starting and executing the queries that you would like to send into
the database. In most cases, you only need to add something to the statement,
rather than just sending out SELECT, such as the brand of the item that you
would like, and then use this command.
When you use the SELECT command in a SQL query, you will use four
different keywords. These are termed as the four clauses and must be present
in the query.
SELECT
This command is often combined with the FROM command, so the relevant
information is obtained in an organized and readable format. Use this to help
determine the data that will show up. The SELECT clause will give you
access to the relevant columns in the dataset, and you can use the FROM to
find the exact point you need.
FROM
The SELECT and FROM commands are often used together, and it is
important to use these commands since that is the only way you can search
for the relevant information in the data set. You also need to ensure you use
them correctly. You need to use at least one FROM clause for this to work. A
good syntax that would use both the SELECT and the FROM properly
includes:
SELECT [ * | ALL | DISTINCT COLUMN1, COLUMN2 ]
FROM TABLE1 [ , TABLE2];
WHERE
You use this query when you work with more than one condition in the
clause. For instance, it is the element in the query that can be used to display
the necessary information within the data set. This is based on the
information the user keys into the DBMS. If you are using this feature, the
right conditions are the AND and OR operators. The syntax that you should
use for the WHERE command includes:
SELEC [ * | ALL | DISTINCT COLUMN1, COLUMN2 ]
FROM TABLE1 [ , TABLE2];
WHERE [ CONDITION1 | EXPRESSION 1 ]
[ AND CONDITION2 | EXPRESSION 2 ]
ORDER BY
This clause can be used to arrange the output of any query you run. This
server can be used to determine the format and order in which different
information appears in your output. The default for this query will be
organizing the output going from A to Z, but you can make necessary
changes depending on the type of information you need. The syntax that you
can use for this will be the same as the one above, but add the following line
at the end:
ORDER BY COLUMN 1 | INTEGER [ ASC/DESC ]
These need to be in place if you want to use the right command to ensure the
query will work properly. You also need to pull the right information from
the database you are working with.
Working with Case Sensitivity
If you work with SQL, you do not always have to worry about case
sensitivity. This is a bit different compared to some of the other coding
languages that you will work with. Choose to use either version of the word,
either uppercase or lower case, and it will work in your searches. You can
also choose to look for the statements and clauses you need to get the
relevant information.
There may be several times when case sensitivity is important. One of these
times is with objects of data. The data you often work with will be in
uppercase, and it is important to know how to work with this information. If
you have one user typing in JOHN and another who chooses to go with John,
people may wonder if they mean the same things. So, use the same format
across the entire column.
It is a good idea to use the upper case as it is easier to read. You can use this
information across all the databases you work with. If you choose not to use
upper case, ensure you are using another method that will allow you to
remain consistent and that you use the same format across the entire database.
This is the only way you and everybody who uses the database know how to
work with different options.
Any transactions or queries you work with in the database will be integral
parts of the system. It is also important to ensure you obtain the right
information that works for you. You may feel that this information is not that
important, and the user can find all this information whether or not there is a
good query set up. If you do not use case sensitivity correctly, you will call
for incorrect information and the result will be incorrect. Nobody needs to
type the relevant words in the query search box and find irrelevant
information to their search.
When you work on developing the database, you need to ensure the query
used is set up correctly. This is the only way you can ask your customers to
find the products they need from the database. When they type in boots, they
will not get a bunch of bath toys, for example. This is the best way to keep
your customers happy and avoid making most of the mistakes one makes
from being unable to find the information they need.
You may not be using the database to sell the right products to your
customers. There will, however, be times when you need to use the database
to perform the necessary functions and searches. If they want to get some
other information from the database, such as their account or a list of the
services you can provide, you will still need to have the transaction go as
smoothly as possible. Ensure the database is created correctly and find all the
information you need.
When you set up the queries you need to use within your database, use the
right queries to ensure you do not have any bugs in your code. Whether you
are trying to sell products on the site or you would like to make it easy to
keep track of all the account information for your customers, you will find
that working with making the queries work the right way inside the database
can make it so much easier. Use some steps mentioned in this chapter to learn
how to set the database up to ensure it makes sense. This is the only way you
can make sense of the information you have in the database or data
warehouse.
Chapter Eleven: An Introduction to Joins
SQL uses joins. A join is an instruction that you can use to combine the
information from two different tables. Before you look at the details of SQL
join syntax, let us understand why you would want to use it. Let us look at
two tables that provide information about customers and orders.
Customers
The following table provides information about the customers in a company.
customer_ first_ last_
email address city
ID name name
1 Barbara Kean Barbara.kean@usa.gov 3100 Kean Phoenix
Hwy
2 John Wick jwick@usa.gov 1250 Quincy
Hancock St
3 Thomas Jefferson tjefferson@usa.gov 931 Thomas Charlottesville
Jefferson
Pkwy
4 James Madison jmadison@usa.gov 11350 Orange
Constitution
Hwy
5 James Monroe jmonroe@usa.gov 2050 James Charlottesville
Monroe
Pkwy
The information about every customer is in a separate row. Every column
specifies different information, including their first name, last name, email
ID, and State. Every customer is associated with a primary key called the
customer_ID.
Orders

order_id order_date Amount customer_id


1 07/05/2021 $110.56 2
2 07/08/2020 $77.50 1
3 05/05/2021 $350.54 4
4 07/06/2021 $520.35 3
5 06/06/2021 $37.75 8
6 04/03/2021 $54.60 9

In the above table, every order you make is directly associated with a primary
key, which is order_id. SQL looks only at this information before it performs
any function.
How to Use A Join
Let us assume you want to obtain the required information from the tables
above to identify the orders made by specific customers. You can do this by
combining or joining the tables – order and customer. This can be done by
using the customer_id.
select order_date, order_amount
from customers
join orders
on customers.customer_id = orders.customer_id
where customer_id = 3
In the example above, we are going to combine two keywords or functions
using the JOIN keyword. You should also identify which key (in this
example, we are using customer_id and order_id) you want SQL to use when
you join the tables. This statement must follow the JOIN Statement. The
following will be the result of your query:
order_id order_date order_amount
2 07/05/2021 $110.56
4 05/05/2021 $350.54
In this example, we are using the inner join. The method you use is
dependent on the type of analysis you want to perform on the data. You can
join different tables in multiple ways. In the next section, we will look at left,
right, and full joins. The examples in the next sections will use the customer
and order tables above.
Basic Types of Join
There are four types of joins – left, right, full, and inner. These joins are
different from each other, and the easiest way to explain these differences is
by using a Venn diagram. This diagram will show every logical relationship
that exists between two tables or datasets.
Before you use any JOIN function in your dataset, you need to extract that
information and load it into the relational database. These databases will help
you to query your data from multiple sources. Use an ETL to build the
process or build it manually. Let us assume that we have two different data
sets – tables A and B. Both tables have a relationship, and this is based on the
primary or foreign key. The following diagram represents the result of
combining the tables.

The number of records in the first table matches those in the second table.
These determine the extent to which there is an overlap between the two
tables. Use the different types of joins based on the type of data or the subset
you want to select from the two tables.
You can visualize the four types of joins using the following Venn diagrams.
Inner Join
In this type of join, you will select all the records from both tables A and B if
and only if the condition is met.
Left Join

The left join uses the records from Table A. Some of these records are
selected depending on whether the condition is met.
Right Join
In this type of join, all the records from Table B are selected, with some
records from Table A depending on if the join condition is met.
Full Join

The data from both tables are selected. The output includes all information
from both sets regardless of the condition.
Examples of SQL Join Types
Let us now look at the tables we defined at the start of the chapter to
understand how joins can be used. As mentioned earlier, the relationship
between both tables is defined by the primary key, customer_id, in the
customer table that is a foreign key in the table.
customer_ first_ last_
email address city
ID name name
1 Barbara Kean Barbara.kean@usa.gov 3100 Kean Phoenix
Hwy
2 John Wick jwick@usa.gov 1250 Quincy
Hancock St
3 Thomas Jefferson tjefferson@usa.gov 931 Thomas Charlottesville
Jefferson
Pkwy
4 James Madison jmadison@usa.gov 11350 Orange
Constitution
Hwy
5 James Monroe jmonroe@usa.gov 2050 James Charlottesville
Monroe
Pkwy

order_id order_date Amount customer_id


1 07/05/2021 $110.56 2
2 07/08/2020 $77.50 1
3 05/05/2021 $350.54 4
4 07/06/2021 $520.35 3
5 06/06/2021 $37.75 8
6 04/03/2021 $54.60 9

In the above table, the orders correspond to a customer in the database. If you
look at the table above, you can see a few orders which do not have customer
information against it.
Inner Join
Let us assume you want to extract the relevant information from the tables
above about your customers who have placed orders. We also want
information about the orders placed by that customer. In this situation, it is
ideal to use the inner join since this provides information from the data set
where there is common information.
select first_name, last_name, order_date, order_amount
from customers c
inner join orders o
on c.customer_id = o.customer_id
You will receive the following output:
first_name last_name order_date order_amount
Barbara Kean 07/05/2021 $110.56
John Wick 07/08/2020 $77.50
Thomas Jefferson 05/05/2021 $350.54
James Madison 07/06/2021 $520.35
James Monroe 06/06/2021 $37.75
Note that Thomas Jefferson, George Washington, and John Adams are the
only customers who have placed orders. Thomas Jefferson was the only
customer who placed two different orders.
Left Join
If you only want to use the join to append any information about the
customers to the data set, you can use the left join. You can also set the right
conditions to ensure you only choose those customers who place an order.
This join will return all the records in Table A and records from Table B if
they match the join condition.
select first_name, last_name, order_date, order_amount
from customers c
left join orders o
on c.customer_id = o.customer_id
first_name last_name order_date order_amount
Barbara Kean 07/05/2021 $110.56
John Wick 07/08/2020 $77.50
Thomas Jefferson 05/05/2021 $350.54
James Madison 07/06/2021 $520.35
James Monroe 06/06/2021 $37.75

Since there was no matching record available for James Monroe and James
Madison in the orders table, the left join will return a null value. This
indicates there is no information present in the data you are extracting.
You may also wonder why it is important to use this join. If you want to add
extra lines of code to the data asset, check where you have placed the order.
Doing this also gives you the information about the customers who have
placed these orders.
select first_name, last_name, order_date, order_amount
from customers c
left join orders o
on c.customer_id = o.customer_id
where order_date is NULL
Right Join
The right join is the opposite of the left join. This operator can provide
relevant information about the orders and information about the customers
who made the purchase.
select first_name, last_name, order_date, order_amount
from customers c
right join orders o
on c.customer_id = o.customer_id

first_name last_name order_date order_amount


Barbara Kean 07/05/2021 $110.56
John Wick 07/08/2020 $77.50
Thomas Jefferson 05/05/2021 $350.54
James Madison 07/06/2021 $520.35
James Monroe 06/06/2021 $37.75
NULL NULL 07/21/1795 $25.50
NULL NULL 11/27/1787 $14.40
Since there is no matching record in the order placed between the years 1787
and 1795, the name of the customer is null. Also, note the way the tables
have been joined. If you use a right join to map the customer table to the
order table, the result would be the same as that of the left join. Add another
line to the code to obtain the information about the customers who placed the
order. To do this, use the query “where first_name is NULL.”
select first_name, last_name, order_date, order_amount
from customers c
right join orders o
on c.customer_id = o.customer_id
where first_name is NULL
Full Join
If you want a list of all the records in both tables, you should use the full join.
select first_name, last_name, order_date, order_amount
from customers c
full join orders o
on c.customer_id = o.customer_id
first_name last_name order_date order_amount
Barbara Kean 07/05/2021 $110.56
John Wick 07/08/2020 $77.50
Thomas Jefferson 05/05/2021 $350.54
NULL NULL 07/21/1795 $25.50
NULL NULL 11/27/1787 $14.40
James Madison 07/06/2021 $520.35
James Monroe 06/06/2021 $37.75

What Do You Do Next?


You can use the four types of joins in this chapter and use them to tie up
different types and forms of data. This allows you to ask the right questions
and challenge your data. If you work on large data sets, it does become
difficult to bring information into the data warehouse and database. It is very
difficult to maintain the information in the data set. This is true if you use
different sources to obtain the data. This section will cover integrating data
from different sources into a warehouse or database. It is also important to
maintain the quality and accuracy of the information in the database.
It is easier to work with Joins only on specific data sets since this is easier to
do. In most cases, people use data from different sources to perform better
analysis. For example, suppose you combine marketing data from Facebook
or AdWords and the transaction data from your website or account. In that
case, it becomes easy for you to uncover any actionable insights about the
business. This information can be used to maximize profits.
Having said that, it can be quite difficult to perform joins on different types
of data sets. Therefore, you need to consolidate the data first into a warehouse
or database. As mentioned earlier, you can use an ETL service, or you can
manually extract the data from these sources. You can use different querying
statements to consolidate the information into a database or warehouse.
You can combine the information in different databases and warehouses and
combine the information in different tables. You can obtain this information
from different sources. If you use the Redshift warehouse, you can connect to
different business intelligence tools like Wagon, Looker, and Mode. The data
warehouse also improves the querying speed. You can view, analyze, query,
and visualize the information in the data set using charts and reports.
The objective here is to help you understand that it is difficult to combine
different SQL queries, especially if the data is not stored correctly. The
easiest way to connect the information to different sources is using ETL
services, such as Stitch. Stitch will allow you to connect to different sources
that your business uses and streams all that information into a data warehouse
like Redshift. You can also stream information into the database you have
created for the purpose of analysis.
When you have the right information in the database or warehouse, it
becomes easy for you to understand the different sources of information. This
is the only way you can determine the types of joins you can perform on any
data set within the warehouse.
As mentioned earlier, the information in the company database is often used
to analyze the functions that pose the business a challenge. If you use a SQL
join statement, it becomes easy to source the relevant information from the
data set. Every business uses different sources to obtain the data, and the data
from these sources come in different formats and forms. This is where it
becomes hard for you to join the data. Therefore, it is also important for you
to use an ETL service to make it easier to source relevant information.
Chapter Twelve: An Introduction to Subqueries
Let’s talk about another way to protect the information in the data set from
unwanted actions being performed on the data set. The easiest way to do this
is to prevent any modifications from being made to the data set. You can do
this by normalizing your database. In normalization, you will need to break
one table into multiple tables using one theme. For instance, do not include
product information in the customer table. This is the best way to avoid the
purchase of the product.
Normalizing data sets and data warehouses is of utmost importance. You
need to ensure this is done correctly. This data will be spread across various
tables and often need the write queries to pull information from these tables.
You can do this by using the join operators or relational operators (covered in
previous chapters). These relational operators take the necessary information
from different tables to create one table. The table will have a different
combination of data depending on the operator you are using. Another way to
do this is to pull information from the queries.
If you use nested queries, the outermost statements in the query will have the
necessary subqueries within them. These subqueries serve as an enclosing
statement for lower-level subqueries that are nested within it. You can have
any number of subqueries in the main query since there is no theoretical
limit. It is best to use fewer queries if you want to avoid any difficulties with
implementation.
A subquery uses a SELECT statement, but the query on the outside can
include SELECT, INSERT, UPDATE or DELETE statements. You can also
use a subquery to operate on a table that is different from another table. It is
for this reason that a nested query will allow you to extract information from
different tables.
For example, if you look at the corporate database, you may find a list of
managers over the age of fifty. You can also use joins to find this
information. If you were to use a subquery, you could write the following:
SELECT D.Deptno, D.Name, E.Name, E.Age
FROM DEPT D, EMPLOYEE E
WHERE D.ManagerID = E.ID AND E.Age > 50 ;
Here, D is the alias for the Department table, E is the alias for the Employee
Table.
The Employee table also includes a column labeled ID in the data set, and
this is the primary key. The ManagerID is the primary key in the department
table. This ID is the value of the employee who is the manager of the
department. Using a simple join, you can pair the tables, and using the
WHERE clause, you can filter the necessary information from the data set.
This information will not include the data that does not fit the join criterion.
This parameter list will include the columns DeptNo and Name from the
Department table and the Name and Age columns in the Employee table.
Let us assume you only need the information from the department table, and
you look at the necessary rows for the same. This statement allows you to
obtain information from the table about managers who are over 50. You do
not care who the managers are or what their exact age is. In such cases, use
the following syntax instead of a subquery.
SELECT D.Deptno, D.Name
FROM DEPT D
WHERE EXISTS (SELECT * FROM EMPLOYEE E
WHERE E.ID = D.ManagerID AND E.Age > 50) ;
This query has the following elements:

1. SELECT* in the WHERE clause – this select statement is a


subquery.
2. EXISTS Keyword – one of the many statements used in a
subquery.
Why Should You Use A Subquery?
A subquery can be used instead of a JOIN statement in most cases since the
query is not as complex as the join statement. Determine whether you want to
use a subquery or a join depending on what you are comfortable with. Most
often, people also choose to retrieve the information from other tables using a
join operator. Some people choose to use subqueries instead. There are times
when you cannot use a join statement to obtain the necessary result. In those
instances, it would be best to use a subquery. You can also break the
problems into multiple sub-problems and write multiple statements. You can
execute each of these statements separately based on the information you
need.
What Do Subqueries Do?
Subqueries should be written within an enclosing statement. It is best to write
them within the WHERE clause. The function of a subquery is to set the
search condition for the WHERE clause. Different subqueries give you
different results. Some can be used to produce single values, while others can
be used to compare different values. Other subqueries produce a list of values
that the enclosing statement will use as an input. Some also return Boolean
values as their output.
Chapter Thirteen: An Introduction to Set
Operators
Some SQL queries can be used to obtain meaningful information from the
data set. You can use these operators when you need to use special
instructions. In this chapter, we will look at some common operators:

1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION and UNION ALL
The UNION operator in SQL can be used to combine the results of two or
three different queries. These queries are specific types of SQL statements.
Before we understand the syntax of these operators, bear the following in
mind:
● The UNION operator can only be used with a SELECT operator if the
tables being considered to have the same number of columns and in the
same order
● The columns in the data set should all use a similar data type
Syntax for UNION
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Syntax for UNION ALL
If you want to include duplicate values when you use the UNION operator,
you should include the keyword ‘ALL’ after UNION. The syntax for this is,
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Bear in mind that the headers for all columns should be the same in the
SELECT statements you write.

CustomerID CustomerName ContactName Address City


1 Oliver Twist Oliver Twist Obere Str. New York
57
2 Antonio Antonio Avda. de la Chile
Banderas Banderas Constitución
2222
3 David David Mataderos Barcelona
Copperfield Copperfield 2312
Sample Database
Consider the following database. This is the Northwind database in the
examples in the next section.
Let us look at a sample from the Suppliers table.
SupplierID SupplierName ContactName Address City PostalCode
49
1 Ace Lucius Fox Gilbert New York EC1 4SD
Chemicals St.

P.O.
Arkham Edward
2 Box Chile 70117
Printers Nygma
78934
707
Gotham Alfred
3 Oxford Barcelona 48104
Publishers Pennyworth
Rd.
Example 1
In the example below, we will use the relevant query and operator to obtain
distinct information from the supplier and customer tables.
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
If there is a supplier or customer who is from the same city, this will only be
listed once since this operator only returns the relevant information. If you
want to use all the functions, use the UNION ALL operator.
Example 2
The following example will return all city values, including the duplicate
values from both tables.
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
Example 3
We will now use the UNION operator and the WHERE keyword, and SQL
will return the information needed from the supplier and customer tables.
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Example 4
This example uses the UNION ALL operator and WHERE query to give you
the output of the German cities in the data set, including the duplicates.
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Example 5
SQL will return all the customers and suppliers in both tables.
SELECT 'Customer' As Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;
Example 6
SELECT product_id FROM order_items
UNION
SELECT product_id FROM inventories;
SELECT location_id FROM locations
UNION ALL
SELECT location_id FROM departments;
Example 7
SELECT location_id, department_name "Department",
TO_CHAR(NULL) "Warehouse" FROM departments
UNION
SELECT location_id, TO_CHAR(NULL) "Department",
warehouse_name
FROM warehouses;
INTERSECT
You need to use this operator along with two SELECT statements. The query
only returns the rows that SQL selects by all datasets or queries. If there is a
record present in one result set and not in the other, SQL will not include it in
the results.
The Query
Using the diagram below, you can see how the clause works. This query only
returns the records present in the shaded area since the records you are
looking for are common to both sets. The rules for the INTERSECT clause
are the same as the UNION clause.

Syntax
The syntax is,
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Example 1
Let us now look at a different example. In this, we will use the INTERSECT
operator to return values from more than one column. The query will return
records from the contacts and customers table if and only if the contact_id,
last_name, and first_name match. We will also use the WHERE condition in
the data set, which can be used to filter the records where the details in the
table for the last_name object do not have Anderson. The other condition is
that the customer_id is one where the value is lower than 40.
SELECT contact_id, last_name, first_name
FROM contacts
WHERE last_name <> 'Anderson'
INTERSECT
SELECT customer_id, last_name, first_name
FROM customers
WHERE customer_id < 40;
Example 2
In this example, we are using the supplier_id since it will have the same data
type in both tables. SQL will return the supplier_id from the order and
suppliers table if the ID appears in both result sets.
SELECT supplier_id
FROM suppliers
INTERSECT
SELECT supplier_id
FROM orders;
Example 3
We continue to use the example above and will include the WHERE
condition in the query. The WHERE clause is added to both data sets, but the
SQL query used will only focus on the supplier IDs in the database whose
values are above 78, while the second one looks at the supplier IDs whose
value is above zero.
SELECT supplier_id
FROM suppliers
WHERE supplier_id > 78
INTERSECT
SELECT supplier_id
FROM orders
WHERE quantity <> 0;
Example 4
The example below will look at the ORDER BY clause, which also uses an
INTERSECT clause. The names of the columns used are not the same as the
SELECT statements. Therefore, you can use the ORDER BY clause to refer
to the relevant columns in the data set. We also sort the information or data in
the data set below in ascending order using the parameters company_name
and supplier_name.
SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
INTERSECT
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;
Example 5
SELECT product_id FROM inventories
INTERSECT
SELECT product_id FROM order_items;
MINUS
In this section, we will learn about the MINUS operator. This operator only
returns the values present in one data set. It uses two SELECT statements.
The MINUS operator will give you the values in the first data set that are not
present in the second data set. This is synonymous with the mathematical
operations performed on sets.
The Query
In the image below, you will notice the MINUS query does not return the
values present in the common data set. It is only used to obtain uncommon
information. These records are only present in the first dataset and not the
second. When you use the SELECT statement in the query, ensure you have
the same data types and the same number of fields.

Syntax
The syntax for the minus operator is as follows:
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Example 1
We will use one field from the data sets and use the same data type in the
following example. The MINUS query in the example below can be used to
retrieve the supplier IDs from the data set. These values are present only in
the supplier tables, which means the supplier_id is a primary key, and it is
common to both the data sets. If it is not present in the first data set, the
MINUS query does not return the supplier_id.
SELECT supplier_id
FROM suppliers
MINUS
SELECT supplier_id
FROM orders;
Example 2
SELECT product_id FROM inventories
MINUS
SELECT product_id FROM order_items;
Example 3
In the example below, we will use the ORDER BY clause since there are
different columns in both data sets, which you can use to club the
information.
SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
MINUS
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;
Chapter Fourteen: Rookie Mistakes
People often read about SQL only when they are looking to work with
relational database systems. You would not study SQL for fun. Most
organizations use SQL to build applications that function on databases.
Before you work on developing, you need to create or extract the database
you wish to work on. Several projects often crash far before the code is
written on account of an incorrect database. Therefore, it is important for you
to create or procure the relevant information to make the application work. In
this chapter, you will learn about some rookie mistakes made in the database.
Assuming Your Clients Know What They Need
Clients often call experts to create and design the database, especially if they
are unhappy with the existing solutions. They often believe that they have
identified both the problem and the solution, and all they would need to do is
tell you what needs to be done. If you give the clients exactly what they need,
you end up digging your own grave. Unfortunately, most users and managers
do not have the necessary information or skillset to enable them to identify a
problem accurately. They also do not have the needed skills to identify and
design the solution. You need to convince the client that you have the skills
necessary to identify the issues within the system and to analyze the issues
within the system, you will need to go over the design of the database. There
is a cause hidden behind the symptoms.
Isolating Database Table Designs
If you incorrectly identify any data objects and the relationships between
these objects, it can introduce bugs and errors in the database. This can
destroy the validity of your results. To design the right database, you need to
consider the organization of the objects, data, and function in the database
and see how each relates to the other. You do not have one design alone, and
therefore, it is important to determine the design and its appropriateness. You
must understand your client’s requirements before you set up the database.
Bear in mind that not everybody is an expert at something. A database
management system works in a teleprocessing environment different from
other systems that work in a server or client environment. It is also important
to have the right people to help you in environments where you have shared
resources and distributed databases. The one or two systems that you are an
expert in may not be the best for the job at hand. It is important to choose the
right architecture, so anybody you pass the work on to can work with the
database. It is better to do the work right by choosing the right environment
to serve your clients’ needs.
Ignoring the Scope of the Project
When you work on developing new applications or databases, you need to
understand and define the scope of the application. There are times when the
client forgets to tell you something. Over the course of the job, new
requirements will crop up, and these will need to be tacked onto the project.
Ensure everything you are asked to do is delivered in writing before you start
working on the project. If you are paid based on a project instead of an hourly
basis, these new requirements will turn what was once a profitable job into a
huge loss for you. If any new additions crop up, these would need to be
compensated for in both time and money.
Considering Technical Factors Alone
An application developer often considers potential projects based on
technical feasibility. The developers also base their time and effort estimates
on that determination. Some aspects can have a major effect on the project:

1. Issues of cost maximums


2. Resource availability
3. Schedule requirements
4. Organization politics
These issues may turn a project that is technically feasible into a nightmare.
Ensure you understand all relevant factors before you start any development
project. You may decide that it makes no sense to proceed; you are better off
reaching that conclusion at the beginning of the project than after you have
expended considerable effort.
Using only one Development Environment
You may have spent quite some time trying to understand and become
proficient in the use of the application development environment or the
DBMS. Your favorite environment will have its own set of weaknesses and
strengths, regardless of which environment you wish to use. You may come
across a development task where you have some heavy demands to meet.
This indicates your preferred development environment is weak. Rather than
working on something that is not the best way to develop the application, you
can just bite the bullet. The following are your only options:

Tell your clients you can't complete the task unless you have a tool
which you know how to use or
Learn how to use the appropriate tool, so you can use it to meet
your clients’ requirement
You can also suggest to the client that they hire someone who can work on
the project better and be more productive. It is only when you behave
professionally that you can gain your clients’ trust. However, do check with
your organization before you make this suggestion to the client.
Skipping Testing
Most database applications are complex, and there will be some bugs in the
code, which is normal for any application. Your application is bound to
contain errors and bugs despite numerous tests. It is important to ensure you
continue to perform testing on the application before you launch the
application to the public. You need to fix anything that others find before the
product goes officially into use.
Bear in mind that everybody is bound to make mistakes. The best developer
and designer can make mistakes and miss important aspects to consider.
These may be evidence to someone who is looking at the design with a
different view. If you must present your work in front of a design reviewer,
you must be more disciplined. This will help you avoid numerous problems
that you may otherwise have experienced. Ensure you have a competent
professional review your proposed design before you start development.
No Documentation
Most people believe the application they develop is perfect, which means
they think the code does not have to be looked at or changed in a few weeks
or months. It is important to remember that change is constant. Six months
from now, you will not remember why you designed things the way you did
unless you carefully document what you did and why you did it that way. If
you transfer or move to another department without access to the SQL code,
your replacement will not know what is happening in your code. You need to
document the code so that a person who comes in place of you will know
exactly what needs to be done. Do not just document your work adequately
— over-document your work. It is best to add more details to the
documentation if you think it is relevant. If you come back to work on this
project, you will be glad that you have documented the necessary
information.
Never taking feedback
The first inclination would be to listen to your managers, especially those
who hire you. The users themselves do not have any clout. Alternatively, you
can ignore managers in some situations, too. Most managers do not know
what the clients or users need. Do not automatically assume that you know
more than your client groups about what they need. Most data entry clerks do
not have the necessary information, and most managers only have a decent
understanding of what needs to be done. If you isolate yourself and do not
take inputs from the other members of the group, it will result in a system
that cannot be used to solve problems. Applications that work correctly but
do not solve the client’s problem are useless for everybody.’
Chapter Fifteen: Retrieving Information From
Databases
The SQL SELECT statement is your tool to dig up relevant information in
the database. Even if you have a clear idea of what you want to retrieve,
translating that idea into SQL can be a challenge. If your formulation is just a
little off, you may end up with the wrong results — but results that are so
close to what you expected that they mislead you. If you want to reduce the
chances of using incorrect queries, use the principles below:
Verify the Structure of the Database
If you retrieve information from the database and the results you obtained do
not seem accurate, perform the necessary tests to ensure the design is
accurate. You also need to get rid of any poorly designed databases. Good
design is a prerequisite of data integrity.
Test the Database
Once you create the database, you need to create a test database. The latter
should have the same structure as the former but only represent some parts of
the data you will use. Choose the data so that you know in advance what the
result of your query should be. Now run the query on the test database and
see if the relevant details are extracted. If it does not, reformulate your query.
If you have developed the right query, restructure the database so it works
correctly.
Build several sets of test data and be sure to include odd cases, such as empty
tables and extreme values at the very limit of allowable ranges. Do not try to
think of various unlikely scenarios and look for the proper behavior when
you run a query. When you check the data for any unlikely cases, you may
gain insight into problems that are more likely to happen and rectify the
situation.
Always Check Queries with Subselects
Since subselects often entangle information taken from one table with the
data from another, the use of these tables is often incorrect. Ensure the data
used in the inner SELECT functions is retrieving relevant information, which
the first SELECT function can use. This is the only way you can obtain the
necessary information. If you have more than two subselects in your function,
you need to be careful about what information each SELECT is trying to
retrieve.
Summarize Data using GROUP BY
Let us assume you have a table called NATIONAL with the names of the
player, team, and the number of home runs hit. We will look at the players in
the national basketball league. You can retrieve the team homer total for all
teams with a query like this:
SELECT Team,
SUM (Homers) FROM NATIONAL
GROUP BY Team ;
This query will list the team members followed by the number of runs every
member in the team hit.
Double Check Queries with JOIN
JOINs are notorious for being counterintuitive. If your query contains one,
make sure that it’s doing what you expect before adding WHERE clauses or
other complicating factors.
Understand Clause Restrictions
Suppose that you want a list of National League power hitters. Consider the
following query:
SELECT Player, Team, Homers FROM NATIONAL
WHERE Homers >= 20
GROUP BY Team ;
In most SQL implementations, this query will throw an error. Most columns
used to group data sets or those used in a function appear in the SELECT
statement. The following formulation works:
SELECT Player, Team, Homers FROM NATIONAL
WHERE Homers >= 20
GROUP BY Team, Player, Homers ;
Since the columns you want to use appear in this clause, you need to run the
query to obtain the required results. This formulation sorts the resulting list
first by Team, then by Player, and finally by Homers.
Handle Error Conditions with Care
Regardless of whether you make ad hoc queries from the embedding queries
or console in an application, SQL returns an error message that you can add
to the end of the SQL query. You can decide what to do next based on the
message returned and then take appropriate action at the console. In an
application, the situation is different. The application user probably does not
know what action is appropriate. Ensure you have an extensive error handling
code in your query into your application to cover conceivable errors that can
occur. It is important to learn how to write this code because it will take a lot
of time, but it is best to do this to prevent any errors that make your screen
hang.
Always Use Parentheses for Logical Operators
When you mix the AND and OR logical operators, SQL struggles to process
this information correctly. In such situations, you should use parentheses.
These parentheses, when used in complex statements, ensure that you get the
desired result. The few extra keystrokes are a small price to pay for better
results. Parentheses also make it easier for the DBMS to ensure the keywords
are applied to the right terms and expressions.
Control the Retrieval Privileges
We will look at database security in further detail in the upcoming chapters,
but it is important to avoid giving anybody these privileges. It is also
important to use the security features in the database. People often do not use
them because they believe misuse and misappropriation of data to be
something that only happens to other people. Do not let the data go missing.
Maintain and establish security for every database you own, to ensure the
values are not changed.
Create a Regular Backup
It is very difficult for you to retrieve information after an earthquake, power
surge, or fire. These phenomena can destroy your data. Therefore, it is
important to make frequent backups and remove the backup media to a safe
place. What constitutes a safe place depends on how critical your data is.
Ensure the drive is fireproof and is present in the same room as your
computer. It might be in another building. You can also keep this data. It
might be in a concrete bunker under a mountain that has been hardened to
withstand a nuclear attack. Decide what level of safety is appropriate for your
data.
Chapter Sixteen: Using SQL With Applications
So far, we have looked at how SQL can be used in isolation. We looked at
different operations in SQL and how those can be used in tables created in
the database. We also looked at the different means to use SQL queries to
extract the required information from tables. If you only wish to learn how
SQL works, you can use this type of learning, but this is not only how SQL
can be used.
The syntax of SQL is close to English, but it does take time for one to master
it. Most people are not familiar with SQL, so the demand for people who
know it is high. If a question about a database comes up, a user will never use
a SELECT statement to answer that question. Application developers and
systems analysts are probably the only people who are comfortable with
using SQL. Most people do not make a career by learning SQL. They do not
use it only to retrieve information and instead develop the necessary
applications that write the necessary queries.
If you want to perform the same operation continuously, you should ensure
that you never have to rebuild that operation from scratch. Instead, write an
application to do the job for you. If you use SQL in the application, it will
work differently.
SQL In an Application
Most people have the incorrect notion that SQL is an incomplete
programming language. If you want to use SQL in an application, you must
combine SQL with any other procedural language like FORTRAN, Pascal, C,
Visual Basic, C++, COBOL, or Java. SQL does have some strengths and
weaknesses because of how the language is structured. This language is
structured differently and has its own strengths and weaknesses. When you
combine the two languages, you can overcome the weaknesses of SQL and
the procedural language.
SQL can be combined with other high-level languages allowing you to build
powerful applications. This application will have a wide range of capabilities.
In the previous chapters, we have typed asterisks. It is never good to use this
asterisk if you write SQL as part of another program or application. Once you
have written the application, you may want to add or delete a column from
the table when it is no longer necessary. When you do this, you change the
meaning of the asterisk. If you use the asterisk in the application, it may
retrieve columns that it thinks it is getting.
This change does not affect any existing program until you must recompile it
to see if there are any bugs. The effect of the asterisk wildcard will then
expand to current columns. This will cause the application to fail when it
cannot identify the bug during the debugging process. Therefore, when you
build an application, refer to the column names explicitly in the application
and avoid using the asterisk.
SQL Embedded Java Program
This section will look at the information you need to embed an SQL query
within a Java application. We will use a PostgresSQL database as the
backend, but you can use any other database as the backend instead of
PostgresSQL. You may need to make some changes on your computer when
you set up the database. The following should be set up on your computer:

1. Java SDK
2. Eclipse DBE
3. Some DBMS
You can embed the SQL queries into a Java application easily. The only
thing you need to do is to use the correct JDBC driver in the system. You will
need this information if you want to connect and work with the database
accurately. This driver is a file that provides the API that you need to use to
write queries in the database. If you are unsure of which driver to use, you
can look for the right one on Google. For Postgres users, the driver can be
found at the following location: http://jdbc.postgresql.org/. Download the
driver into the directory that you will use for the development of the
application. When you download the driver, you can start setting the project
up. The only thing you would then need to do is set the project up before you
use a query to connect to the database.
Project Set Up in Eclipse
The first thing you should do is create a new project in your eclipse and name
it “Database_Tutorial.” Once you set this project up, perform the steps below:
1. Right-click on the name
2. Select the options “Build Path” and “Configure Build Path.”
3. After this, click on the option “Add External Jars” and then browse to the
JDBC Driver.
This is the only way you can set up the environment that will act as an
interface between your database and you.
Create the Database Connection
Now, set the connection to the database you want to use. The first thing you
should do is create an object that is used to connect the catch statements in
the code to the database. This object will catch those statements that cause
some error. You can use the method ‘DriveManager.getConnection’ to
handle the connection made to the database. Using this method, you can also
access the Postgre database if you have the location, username and password
set for that database. Let us assume, for this example, that the user name is
Postgres and the password is pass.
Connection c = null;
try {
// The second and third arguments are the username and
password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localhost/",
"postgres", "root");
} catch (Exception e) {

}
You can run this code on your Java program. If you do not have any errors, it
indicates you have connected to the database. You now need to run a query in
the database to derive the necessary information. You can play with the data
to understand it better.
Let us now perform a simple query on the database. The following code will
print the results to the command line.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Tutorial {
public Tutorial(){
Connection c = null;
try {
// The second and third arguments are the username and
password,
// respectively. They should be whatever is necessary to
connect
// to the database.
c=
DriverManager.getConnection("jdbc:postgresql://localhost/",
"postgres", "root");

String query = "Select * From author";


Statement s = c.createStatement();
ResultSet queryResult = s.executeQuery(query);

while(!queryResult.isLast()){

queryResult.next();
String authorName = queryResult.getString(1)+",
"+queryResult.getString(2)+", "+queryResult.getString(3);
System.out.println(authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Tutorial t = new Tutorial();


}

}
If you read through the above code carefully, you will note the Statement and
ResultSet objects are new objects in your code. These statements allow for
the iteration and the execution of the results in the returned rows. The latter
object can now be used as a cursor and return the rows needed. The result
will then be looped through every row until you reach the end of the data set.
Adding User Input to Queries
Alternatively, you can use a Scanner to include or add any user interactivity.
This Scanner will wait for the first user input and then pass that information
along into a method. It then builds a SQL query to connect to the database. If
you include user inputs into this program, there is a possibility of a SQL
injection. SQL injection is an ethical hacking process, and we will look at this
in detail later in the book.
SQL Embedded C Program
You now know how to connect to your database using procedural language.
In this section, we will look at how you can develop an embedded SQL
program in C. In the following example, we will look at a station and extract
the information for that station. We are going to use the same table we did in
chapter Nine for this.
#include<stdio.h>
#include<string.h>
EXEC SQL BEGIN DECLARE SECTION;
long station_id;
long mon;
float temp;
float rain;
char city_name[21];
long SQLCODE;
EXEC SQL END DECLARE SECTION;
main()
{
/* the CONNECT statement, if needed, goes here */
strcpy(city_name,"Denver");
EXEC SQL SELECT ID INTO :station_id
FROM STATION
WHERE CITY = :city_name;
if (SQLCODE == 100)
{
printf("There is no station for city %s\n",city_name);
exit(0);
}
printf("For the city %s, Station ID is %ld\n",city_name,station_id);
printf("And here is the weather data:\n");
EXEC SQL DECLARE XYZ CURSOR FOR
SELECT MONTH, TEMP_F, RAIN_I
FROM STATS
WHERE ID = :station_id
ORDER BY MONTH;
EXEC SQL OPEN XYZ;
while (SQLCODE != 100) {
EXEC SQL FETCH XYZ INTO :mon, :temp, :rain;
if (SQLCODE == 100)
printf("end of list\n");
else
printf("month = %ld, temperature = %f, rainfall =
%f\n",mon,temp,rain);
}
EXEC SQL CLOSE XYZ;
exit(0);
}
The output will be
For the city Denver, Station ID is 44
And here is the weather data:
month = 1, temperature = 27.299999, rainfall = 0.180000
month = 7, temperature = 74.800003, rainfall = 2.110000
end of list
SQL Embedded FORTRAN Program
SQL queries can also be embedded in a FORTRAN application or program,
but to do this, you need to set the application to use SQL statements. These
embedded statements in FORTRAN should contain one of the following:
● Correct FORTRAN Element Syntax
● Statement initializer
● EXEC SQL
● Statement string
● Any valid SQL statement with blanks as delimiters
● Statement terminator
● End of the source line
You can state the end of the source line using the terminator statement. If you
continue this line, this will be the end of this code.
Examples of Declaration
C Include error handling
exec sql include sqlca
exec sql begin declare section
C Variables of each data type
byte dbyte
logical*1 dlog1
logical*2 dlog2
logical*4 dlog4
logical dlog
integer*2 dint2
integer*4 dint4
integer dint
real*4 dreal4
real*8 dreal8
real dreal
double precision ddoub
parameter (max = 1000)
character*12 dbname
character*12 fname, tname, cname
C Structure with a union
structure /person/
byte age
integer flags
union
map
character*30 fullnm
end map
map
character*12 first
character*18 last
end map
end union
end structure
record /person/ person, ptable(MAX)
C From DCLGEN
exec sql include 'employee.dcl'
C Compiled forms
integer empfrm, dptfrm
exec sql end declare section
external empfrm, dptfrm
SQL Embedded COBOL Program
If you want to perform SQL operations in a system where you use COBOL,
you can use embedded SQL statements to perform those operations. When
you enter these statements, a SQL processor first compiles or preprocesses
these lines of code before the application program compiles them. An
embedded SQL statement works much like a regular SQL statement, except
for some changes being made to how the code is processed. For instance, any
embedded SQL statement you write points to a host variable. You also need
to add an INTO statement to any SELECT statement you write in an
embedded SQL program.
IDENTIFICATION DIVISION.
PROGRAM-ID. TESTALL.
AUTHOR-NAME. ME.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-AT.
OBJECT-COMPUTER. IBM-AT.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE EMPREC
END-EXEC
01 DISP-RATE PIC $$$,$$$,$$9.99.
01 DISP-COM PIC Z.99.
01 DISP-CODE PIC ----9.
01 FAKE-CHAR PIC X.
01 ANSS PIC X.
01 COM-NULL-IND PIC S9(4) COMP.
EXEC SQL
INCLUDE SQLCA
ND-EXEC
PROCEDURE DIVISION.
100-MAIN.
* declare cursor for select
EXEC SQL
DECLARE EMPTBL CURSOR FOR
SELECT *
FROM EMPLOYEE
ORDER BY LNAME
END-EXEC
open cursor
EXEC SQL
OPEN EMPTBL
END-EXEC
MOVE SQLCODE TO DISP-CODE
DISPLAY 'open ' DISP-CODE
* fetch a data item
EXEC SQL
FETCH EMPTBL INTO
:ENO,:LNAME,:FNAME,:STREET,:CITY,
:ST,:ZIP,:DEPT,:PAYRATE,
:COM :COM-NULL-IND
END-EXEC
100-test.
MOVE SQLCODE TO DISP-CODE
DISPLAY 'fetch ' DISP-CODE
* loop until no more data
PERFORM UNTIL SQLCODE < 0 OR SQLCODE = 100
* display the record
MOVE PAYRATE TO DISP-RATE
MOVE COM TO DISP-COM
DISPLAY 'department ' DEPT
DISPLAY 'last name ' LNAME
DISPLAY 'first name ' FNAME
DISPLAY 'street ' STREET
DISPLAY 'city ' CITY
DISPLAY 'state ' ST
DISPLAY 'zip code ' ZIP
DISPLAY 'payrate ' DISP-RATE
IF COM-NULL-IND < 0
DISPLAY 'commission is null'
ELSE
DISPLAY 'commission ' DISP-COM
END-IF
DISPLAY 'Do you want to see the next record? (y/n)'
ACCEPT ANSS
IF ANSS = 'Y' OR 'y'
EXEC SQL
FETCH EMPTBL INTO
:ENO,:LNAME,:FNAME,:STREET,:CITY,
:ST,:ZIP,:DEPT,:PAYRATE,
:COM :COM-NULL-IND
END-EXEC
ELSE
GO TO CLOSE-LOOP
END-IF
MOVE SQLCODE TO DISP-CODE
DISPLAY 'fetch ' DISP-CODE
END-PERFORM .
DISPLAY 'All records in this table have been selected'.
CLOSE-LOOP.
* close the cursor
EXEC SQL
CLOSE EMPTBL
END-EXEC
100-EXIT.
STOP RUN.
Chapter Seventeen: Database Security
Users are a threat to the database and its integrity. Some users should never
have access to data, and others should only have restricted access to the data.
Identify the best way to classify the users into various categories to ensure
every user in the database has access to privileged or classified information.
If you create or develop any schema in the database, you can determine who
the owner of that schema is. If you are the owner, you can determine whom
you want to have access to this schema. If you do grant some privileges, you
can use some commands to withhold them, as well. As the owner of the
schema, you can also decide if you want to revoke the access that somebody
has to your database. Every user must pass an authentication process before
he can access the data he needs to. This process should help you identify the
user. This procedure is dependent on the implementation.
SQL gives you the capability to protect the following database objects:
• Views
• Columns
• Tables
• Character Sets
• Domains
• Translations
• Collations
There are various types of protection you can add to the SQL query, and
these include adding, seeing, deleting, modifying, using, and referencing
databases. You can also use different tools that are associated with protecting
the queries.
Using the GRANT statement, you can give someone access to the data in
your database, while the REVOKE statement can remove that access. When
you control the use of this statement, the DCL controls allow you to see the
database object such as a table, column, or view. If you control the use of the
INSERT statement, a command determines who adds more rows or columns
to a database. Restricting the use of the UPDATE command to authorized
users controls the modification which can be made to the table rows. If you
restrict the DELETE command to certain users, it can help you protect your
data from accidental deletions.
A foreign key is another table’s primary key. Where a database has one, a
constraint needs to be added to the first table, ensuring a reference to the
information in the other table. When a table references another table, the
second table’s information can be used to determine information from the
first table. If you own the second table, it's entirely possible that you may
want to stop this kind of snooping from happening, and this is done by using
the GRANT REFERENCES statement. The next section discusses renegade
references and how to prevent them with the GRANT REFERENCE
statement. When the GRANT USAGE statement is used, you can control
who uses or sees the data held in a collation, domain, translation, or character
set.
Delegating Responsibility
It is important to secure your system by restricting access privileges granted
to users on the database. Some people will reach out to you to understand
their access or lack thereof. You can delegate the responsibility of handling
database security to people from within the information security team. You
can do this easily using different commands. Consider the examples below:
GRANT UPDATE
ON RETAIL_PRICE_LIST
TO SALES_MANAGER WITH GRANT OPTION
Using the above example, you can give the sales manager the authority to
update the retail price list. This will give the sales manager the authority to
grant this privilege to users who need this access, as well. Using this form of
GRANT gives people a lot of access, but you should only grant this privilege
to the relevant people. It is also important for you to trust who you give any
other privileges to.
Be very careful before you run the following query:
GRANT ALL PRIVILEGES
ON FOUR_STAR
TO BENEDICT_ARNOLD WITH GRANT OPTION
Referential Integrity
Most people think they can protect their database if they restrict the use of
viewing, modifying, creating, and deleting functions in the dataset. Against
most threats, you are. If you have an experienced hacker, you can ransack the
information in the dataset using indirect methods.
If a relational database is designed correctly, it will have referential integrity.
This indicates that the data in any table in the database will be consistent with
the information present in other data tables. When you use any database with
a referential integrity constraint, it becomes easy to create tables that use a
foreign key which is the primary key in another table. This column can then
be used as a link to obtain any confidential information about the data.
For example, let us assume you are an analyst on Wall Street. Most people
believe in the stock picks and the accuracy of those picks. So, when you
recommend a stock to any people who trust you, they will buy that stock.
When they do this, the value of that stock automatically increases. Let us
assume you maintain this information in a database, and the most relevant
information is present in the table FOUR_STAR. If you do release a
newsletter, you need to choose to avoid letting the information leak out of the
database. This would mean you need to restrict access to the database.
When you create new tables in the dataset, you are going to leave yourself
vulnerable. The hacker can create a new table that uses the stock name field
of FOUR_STAR as a foreign key, as shown in the following command
example:
CREATE TABLE HOT_STOCKS
(
Stock CHARACTER (30) REFERENCES FOUR_STAR
);
Now the hacker can insert the name of the stock on the NASDAQ, New York
Stock Exchange, and American Stock Exchange into the table. Using these
inserts lets a hacker know the stocks in the table that match the stocks in your
confidential database. It does not take too long for the hacker to determine the
quantity of information one can extract from this exchange.
Protect yourself from different hacks using the example above. You mustn't
use statements like the following:
GRANT REFERENCES (Stock) ON FOUR_STAR TO
SECRET_HACKER;
You should avoid granting any privilege to a user who you think will abuse
their responsibility. It is true people do not have a stamp on their forehead to
determine if they are trustworthy or not. For instance, if you do not lend your
new car to your friend on a long trip, it indicates you are not confident about
the person. The same goes for the REFERENCES in the table. If you are not
confident about whom you share the REFERENCES privileges with, do not
share it with them.
The previous example has a good reason for why one should maintain the
right information in the data set, especially if you were to maintain careful
control of various reference privileges. The following list describes two other
reasons for careful control of REFERENCES:
• Let us assume you specify a constraint in HOT STOCKS using the
RESTRICT option. If another user were to delete a row from the table
in your data set, the database would restrict him. This happens since the
user will violate a referential construct
• If you want to destroy the table using the DROP command, you must
obtain the right information which allows you to get the other user to
get rid of their tables or constraints
To conclude, when you give another individual the right to specify the
integrity constraints to your dataset, you leave your data open to a security
breach. This also means the user may get in your way.
Chapter Eighteen: SQL Injections
Most businesses employ ethical hackers to help them secure their systems
from any vulnerabilities. Some hackers use SQL to inject malware into the
system, and the easiest way for them to do this is through an SQL injection.
This is an attack on a program or ticket. This injection modifies the database,
and this makes it difficult for you to obtain the desired results. You can use a
SQL injection on any field or table in the database, depending on what kind
of input you key into the program before sending the injection to the system.
Therefore, you need to find the right people to set up your system and
network security.
Avoid using some combinations and commands in the same query. For
example, avoid using the following command:
String query = "Select * From author Where authorid ="+id_input;
You can enter any user information using the query above. The SQL
compiler will then concatenate the input using the query. This is a terrible
thing to do since it allows you to add or drop tables based on the instructions
you give to the user. For instance, if a user were to use the command “1.
Drop table author,” the query handler will delete the author table because that
is how the SQL compiler reads the function. If you want to prevent this from
happening, you should include a check command for every command you
write in the query. This compiler needs to look for the user input as an integer
value.
You can use the method called PreparedStatement in Java. This command
makes it hard for hackers to insert any malicious commands into the
statement. It converts the query being used into parameters, thereby
preventing a successful hack. In the example below, we will use two
functions with the same task. One of the functions uses the prepared
statement to ensure any integer value in the code is used to concatenate only
the input statement into the query.
// This query prevents a SQL injection attack through parameterization
using both checking and forcing.
public void input(String input){
Connection c = null;
try {
// The second and third arguments are the username and
password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localhost/",
"postgres", "root");

String query = "Select * From author Where authorid = ?";


PreparedStatement s = c.prepareStatement(query);
s.setInt(1,Integer.parseInt(input));
ResultSet queryResult = s.executeQuery();

while(!queryResult.isLast()){

queryResult.next();
String authorName = queryResult.getString(1)+",
"+queryResult.getString(2)+", "+queryResult.getString(3);
System.out.println(authorName);
}

} catch (Exception e) {
e.printStackTrace();
}

// DOES NO SUCH ERROR CHECKING AND JUST CONCATS


THE INPUT STRING TO THE QUERY. VERY BAD!!!
public void inputBad(String input){
Connection c = null;
try {
// The second and third arguments are the username and
password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc:postgresql://localhost/",
"postgres", "root");

String query = "Select * From author Where authorid =


"+input+";";
Statement s = c.createStatement();
ResultSet queryResult = s.executeQuery(query);

while(!queryResult.isLast()){

queryResult.next();
String authorName = queryResult.getString(1)+",
"+queryResult.getString(2)+", "+queryResult.getString(3);
System.out.println(authorName);
}

} catch (Exception e) {
e.printStackTrace();
}
}
The method PreparedStatement is used to set a query up, so it uses any value
you enter as part of the query. This query replaces the values in the query
with a question mark. You will notice the code uses the select query from a
different table. This indicates the compiler will replace all fields in the table
using a question mark. You can use the set method to fill any missing values
in the data. In the above code, we use the command
setInt(1,Integer.parseInt(input)). This method tells the object in the query that
the first variable entered should be an integer. This method also can be used
to cover different types of injections. Bear in mind that you should use the
right queries and statements to eliminate any injection attacks.
Chapter Nineteen: Exercise
This chapter will look at some exercises to assess how much you have
learned from the book. It is best to answer these questions before you begin
working on SQL on your systems. You can also use these questions to help
you master SQL before applying for a job in the field. Use these questions to
master the subject.
The solutions are given at the end of the chapter, but it would be best to
answer them yourself before you look at the solution.
Table 1 - Worker
RKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE
001 Jonathan Fairchild 500000 2020-02-20
002 Harry Potter 60000 2015-06-20
003 Brent Mead 500000 2021-02-20
004 Graziela Cairney 400000 2019-03-25
005 George Weasley 300000 2016-06-12
006 Fred Weasley 100000 2018-09-12
007 Emma Granger 755000 2009-01-20
008 Ronald Grint 100000 2015-03-12

Table 2 – Bonus
WORKER_REF_ID BONUS_DATE BONUS_AMOUNT
1 2020-02-20 50000
2 2015-06-2 30000
3 2021-02-20 40000
4 2019-03-25 45000
5 2016-06-12 35000
6 2018-09-12 25000
7 2009-01-20 60000
8 2015-03-12
Table 3 – Title
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Manager 2020-02-20
2 Executive 2015-06-2
3 Executive 2021-02-20
4 Manager 2019-03-25
5 Asst. Manager 2016-06-12
6 Executive 2018-09-12
7 Lead 2009-01-20
8 Lead 2015-03-12

You need to run the following query to prepare your sample data.
CREATE DATABASE ORG;
SHOW DATABASES;
USE ORG;

CREATE TABLE Worker (


WORKER_ID INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);

INSERT INTO Worker


(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY,
JOINING_DATE, DEPARTMENT) VALUES
Enter the names of the employees and the other details in the above
table.
CREATE TABLE Bonus (
WORKER_REF_ID INT,
BONUS_AMOUNT INT(10),
BONUS_DATE DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);

INSERT INTO Bonus


(WORKER_REF_ID, BONUS_AMOUNT, BONUS_DATE)
VALUES
//Add the relevant details from the table above
CREATE TABLE Title (
WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);

INSERT INTO Title


(WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM)
VALUES
//Add the relevant information from the table above
Once you feed this data into the database, work on the following questions to
determine how well you have understood the concepts.

1. Write a query to print the first name of the user from the worker
table using an alias.
2. Write a SQL query to list the first name of the users from the
worker table in the upper case
3. Write a query to obtain the list of unique values in the department
in the worker table in the database
4. Write a query to print the first three characters in the first name
column in the worker table
5. Write a query to print the position of a specific alphabet in the first
name column of the worker table
6. Write a query to remove any whitespaces from the right side of the
department column in the worker table
7. Write a query to remove any whitespaces from the left side of the
department column in the worker table
8. Write a query to obtain the list of unique workers in the table
based on the department and print the length of their names
9. Write a query to print the first name of all users on the worker
table and replace the alphabet A with H
10. Write a query to print the first and last names of
the users in the worker table into one column called complete
name. You can use the space character to separate the names
11. Write a query to print the details of workers from
the table by ordering the data in ascending order of the first name
12. Write a query to print the names of all users
whose names are present in the worker table and order the list in
ascending order by the first name and descending order of the
department
13. Write a query to print details of all workers
whose first names are “Emma” And “Ronald” From Worker
Table.
14. Write a query to print the names of workers,
excluding first names “Emma” And “Ronald” From Worker Table.
15. Write a query using which you can print the
details of all workers who work in the Admin department
16. Write a query using which you can print the
details of workers whose name ends with the alphabet A
17. Write a query to generate the output of the list of
workers whose first name ends with the alphabet H and has Six
alphabets
18. Write a SQL query to obtain the list of users who
joined the organization in February 2014
19. Write a query to print the number of users who
work in the Admin department
20. Write a query that obtains the names of all
workers with a salary greater than 50 000 and less than 100, 000
21. Write a SQL query using which you can obtain
the list of users and their details if their salary lies between
100,000 and 500,000
22. Write a query to obtain the details of the number
of employees in every department. Print the output in the
descending order
23. Using a SQL query, print the information of the
workers in the table who also work as managers
24. Write a SQL query to obtain the duplicate data in
data based on specific columns in the data set
25. Write a query that shows the odd-numbered rows
in the table
26. Write a query to obtain the even-numbered rows
in the table
27. Write a query that can be used to copy the format
and information of one table to create another table
28. Write a query to obtain the common records for
two tables in the database
29. Write a query to obtain records from one data
table which is not present in the source data table
Solutions
In this section, we will look at the solutions to the questions in the previous
section. These are not the only ways to perform any functions. You can use
different methods.

1. Select FIRST_NAME AS WORKER_NAME from Worker;


2. Select upper(FIRST_NAME) from Worker;
3. Select distinct DEPARTMENT from Worker;
4. Select substring(FIRST_NAME,1,3) from Worker;
5. Select INSTR(FIRST_NAME, BINARY'a') from Worker where
FIRST_NAME = 'Fred';
6. Select RTRIM(FIRST_NAME) from Worker;
7. Select LTRIM(DEPARTMENT) from Worker;
8. Select distinct length(DEPARTMENT) from Worker;
9. Select REPLACE(FIRST_NAME,'a','A') from Worker;
10. Select CONCAT(FIRST_NAME, ' ', LAST_NAME)
AS 'COMPLETE_NAME' from Worker;
11. Select * from Worker order by FIRST_NAME asc;
12. Select * from Worker order by FIRST_NAME
asc,DEPARTMENT desc;
13. Select * from Worker where FIRST_NAME in
('Emma','Ronald');
14. Select * from Worker where FIRST_NAME not in
('Emma','Ronald');
15. Select * from Worker where DEPARTMENT like
'Admin%';
16. Select * from Worker where FIRST_NAME like
'%a%';
17. Select * from Worker where FIRST_NAME like '%a';
18. Select * from Worker where FIRST_NAME like
'_____h';
19. Select * from Worker where SALARY between
100000 and 500000;
20. Select * from Worker where year(JOINING_DATE) =
2014 and month(JOINING_DATE) = 2;
21. SELECT COUNT(*) FROM worker WHERE
DEPARTMENT = 'Admin';
22. SELECT CONCAT(FIRST_NAME, ' ',
LAST_NAME) As Worker_Name, Salary
FROM worker
WHERE WORKER_ID IN
(SELECT WORKER_ID FROM worker
WHERE Salary BETWEEN 50000 AND 100000);

23. SELECT DEPARTMENT, count(WORKER_ID)


No_Of_Workers
FROM worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;

24. SELECT DISTINCT W.FIRST_NAME,


T.WORKER_TITLE
FROM Worker W
INNER JOIN Title T
ON W.WORKER_ID = T.WORKER_REF_ID
AND T.WORKER_TITLE in ('Manager');
25. SELECT WORKER_TITLE, AFFECTED_FROM,
COUNT(*)
FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM
HAVING COUNT(*) > 1;

26. SELECT * FROM Worker WHERE MOD


(WORKER_ID, 2) <> 0;
27. SELECT * FROM Worker WHERE MOD
(WORKER_ID, 2) = 0;
28. SELECT * INTO WorkerClone FROM Worker;
29. (SELECT * FROM Worker)
INTERSECT
(SELECT * FROM WorkerClone);

30. SELECT * FROM Worker


MINUS
SELECT * FROM Title;
I hope the questions were easy for you to answer and gave you an idea of
how well you understood the concepts of SQL. If you cannot answer these
questions, I suggest you go through the book another time to understand the
concepts better. You can also use the Internet to get more practice questions.
There are numerous tutorial websites and forums you can use to improve
your understanding.
Chapter Twenty: Case Study
Beginners often wonder about the different methods they can use to solve
problems. You need to combine the information from different tables to
obtain the final result. The interesting thing is that there are three ways to
perform this action, and we have covered them throughout the book – joins,
set operators, and subqueries. This is a comprehensive problem, and it is a
great way to understand different SQL functions and queries. Let us now look
at the question that was asked.
A data table provides information about various events, and there are a series
of fields that can be included in this table. This information includes reason
code, duration, and status code. The database also provides information about
different statuses. This table has a field that is common to the main table.
This allows the user to look at the status and not the code of the event. There
are only four statuses into which you can categorize an activity – spare,
ready, early, and down. There is a reason associated with each of these
statuses. The reason associated with the status can be the same for more than
one event. This source database has four tables for every status where the
reasons for the status are stored. There is another field that can be used to link
the code to the reason. This indicates the status field is empty in the data set.
If you are looking for information about a specific status in the main table,
your query should allow you to retrieve the name of the event that has led to
the delay. The query should do the same thing if the status is down. This
query will look for any event in different tables with the status column in
them.
The objective here is to create the right results that do not use the codes using
various descriptions.
Let us now look at the overall view of these tables and then look at the
problem that allows you to match the tables. It is also important to use a
combination that allows you to match the tables based on the value of the
status variable.
You can derive the status name if you use the inner join function to perform
this function. It is difficult to display the reason name. When you put your
mind to it, you will realize that the answer always hinges on how well you
understand the different reason tables. If you use the right statements to treat
every table in the dataset in the same way, you can use the join function to
work on them together. This is the best way to avoid any duplicate entries in
the output.
If you consider this, you know there is an alternative – you can use a union to
perform the function. Each table has a similar structure, making it easier for
you to create the derived tables. This table will result from the combination of
the reason tables that can then be joined to the original table using the ready
and status code. This is only going to prevent the need to match the four
tables.
You can break this problem down into the following segments:

1. Use a join on the status table to obtain a name.


2. Incorporate the result in the previous step into the final query
using a subquery
3. Use a UNION to combine the four reasons tables and avoid the
need to use conditional matching
Now, let us look at each of these problems in order.
Using INNER JOIN to Identify the StatusName
Understand that the StatusName variable from the tables is important to
download into the derived table as the output. To do this, use the INNER
JOIN. We looked at inner joins earlier in the book, and if you looked at it,
you know the output is only obtained if the condition is met. This is also the
most common method used in queries. You know, an inner join can be used
to match the primary and foreign keys. It is only when these keys match that
the query returns the required rows. This is what you have in the problem.
The primary key is the StatusCode in the status table. It is important to match
the code with the foreign key, which is the StatusCode, from the event table.
You do not have to worry about the query returning multiple rows since the
status code is unique to every record in the status table. You can do this using
the query below:
SELECT EventID,
Duration,
S.Name
FROM Event E
INNER JOIN Status S
ON E.StatusCode = S.StatusCode
You must do more than this since you should include the reason name in the
output. This is the best place to begin.
Combining Reason Table Rows Using UNION
The UNION operator is a statement you can use to combine different rows in
a single table into one result. A join combines columns from different tables
into distinct rows for each primary key. Using the UNION operator, you can
add rows from every table into a derived table. If you apply this union clause,
you should combine the rows from the tables. You may also notice that you
are using a static column that will correspond to the other information in the
tables. What I mean by a static column is that it is a fixed value. When you
do this, you can associate the rows in every table with a single code. Bear in
mind this is the important part you need to overcome. We could not identify a
way to match the records in every table by using a status code. Use the reason
and status codes to match different rows.
SELECT 'R', ReasonCode, Name
FROM ReadyReason
UNION
SELECT 'D1', ReasonCode, Name
FROM DelayReason
UNION
SELECT 'S', ReasonCode, Name
FROM SpareReason
UNION
SELECT 'D2', ReasonCode, Name
FROM DownReason
Using a Subquery to Produce the Final Result that Includes the
Derived Table
When you create the union, you can match the output with the various tables
and manage the reasons. You do not have to work with the status code and
map that code to the reason before you can get the name of the reason.
Instead, use the standard INNER join and match the derived table with the
reason table. Use different queries to match the status code and reason code
to identify the reason for the output. It is also important for you to wonder
how you should include this in your query.
It is easy to do this using a subquery. As mentioned earlier, a subquery is a
query that is defined within another query. There are various places in this
query where you can use a subquery. It is best to use this within the FROM
since this statement refers to the derived tables. You now have a derived table
that is based on the result or output of the UNION function. A derived table
is enclosed in a parenthesis, but it is also given a name.
If you look at the query closely, you will note the UNION result is SQ, and
the fields in the output with static variables are all assigned a column name.
This is only done to ensure you use the correct references in the query when
you write the join condition. This is how it becomes easier for you to read
and follow the SQL query. Look at the following code:
SELECT EventID,
Duration,
SR.ReasonName
FROM Event E
INNER JOIN
(
SELECT 'R' as StatusCode, ReasonCode, Name
FROM ReadyReason
UNION
SELECT 'D1', ReasonCode, Name
FROM DelayReason
UNION
SELECT 'S', ReasonCode, Name
FROM SpareReason
UNION
SELECT 'D2', ReasonCode, Name
FROM DownReason
) SR
ON E.StatusCode = SR.StatusCode AND
E.ReasonCode = SR.ReasonCode
If you want to use the inner join, it will take the following form: INNER
JOIN {table} ON {match conditions}. In the example above, the table you
obtained will replace the initial table. This is how the function will work well.
Final Query
The final result will require you to combine multiple solutions from the
above sections. From the above solution, I am sure you know it is simple to
work with queries. It is important to maintain and follow the syntax, but the
objective is straightforward. It may be difficult for you to understand the
solution when you put everything together. I hope you understood how we
came to the final query.
SELECT EventID,
Duration,
S.Name as [Status Name],
SR.ReasonName as [Reason Name]
FROM Event E
INNER JOIN Status S
ON E.StatusCode = S.StatusCode
INNER JOIN
(
SELECT 'R', ReasonCode, Name
FROM ReadyReason
UNION
SELECT 'D1', ReasonCode, Name
FROM DelayReason
UNION
SELECT 'S', ReasonCode, Name
FROM SpareReason
UNION
SELECT 'D2', ReasonCode, Name
FROM DownReason
) SR
ON E.StatusCode = SR.StatusCode AND
E.ReasonCode = SR.ReasonCode
Now, we have all the information we need. I hope you understand the use of
joins, operators, and subqueries. Look at the final query. This query has three
simple concepts that were used. You can use these concepts when you do not
know where you should begin. Understand the problem first and break it
down into simpler and understandable problems before you look at the entire
problem.
Conclusion
We have now come to the end of the book. I thank you once again for
choosing this book to learn more about SQL queries and how you can
develop complex queries.
Lots of data flows in from various sources, and most businesses use this data
to make business decisions to increase revenue. If you own a business, you
know how difficult it is to make sense of the information. It is also important
for you to collect and understand the information to make the right decisions
for your business. It is also important to learn where and how to store this
information to ensure you have access to the data whenever you need it. You
may be holding onto the personal information of your customers or
employees, but you need the information to help you find a way to optimize
sales. You need to have a database that is easy for you to use.
Throughout the book, we looked at different SQL queries and how you can
use them to manage the data you collect. SQL is a simple language, and it is
easy for a beginner to develop complex SQL queries using which you can
extract the relevant information from the data collected. Use the queries
mentioned in the book to gather better insights from data. Use this book as
your guide to organize and access your data anytime you need to.
References
12 Benefits of SQL (Structured Query Language) | Systematix Training. (n.d.). Www.systematix.co.uk.
https://www.systematix.co.uk/sql/12-benefits-of-sql-structured-query-language

Db2 11 - Application programming and SQL - Fortran applications that issue SQL statements. (2016,
April 1). Www.ibm.com.
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/apsg/src/tpc/db2z_sqlstatementsfortran.html

Embedded SQL statements in FORTRAN applications. (2014, January 8). Www.ibm.com.


https://www.ibm.com/support/knowledgecenter/SSSNY3_10.1.0/com.ibm.db2.luw.apdv.embed.doc/doc/c0006166.html

Embedded SQL. (n.d.). Www2.Cs.sfu.ca.


http://www.cs.sfu.ca/CourseCentral/354/zaiane/material/notes/Chapter4/node33.html

Embedded SQL Tutorial: How to write Java applications that need a database. (2012, March 2).
Georgedittmar. https://georgedittmar.wordpress.com/2012/03/02/embedded-sql-tutorial-how-to-write-
java-applications-that-need-a-database/

Is it possible to use Joins, Subqueries, and Unions in one Query? (2015, May 8). Essential SQL.
https://www.essentialsql.com/a-case-study-on-how-to-use-joins-subqueries-and-unions-to-combine-
data-in-sql/

SQL Arithmetic Operator. (n.d.). W3resource. https://www.w3resource.com/sql/arithmetic-


operators/sql-arithmetic-operators.php

SQL - Operators - Tutorialspoint. (n.d.). Www.tutorialspoint.com.


https://www.tutorialspoint.com/sql/sql-operators.htm

SQL SELECT Statement - Dofactory. (n.d.). Www.dofactory.com.


https://www.dofactory.com/sql/select

SQL | Join (Inner, Left, Right and Full Joins) - GeeksforGeeks. (2019, January 9). GeeksforGeeks.
https://www.geeksforgeeks.org/sql-join-set-1-inner-left-right-and-full-joins/

You might also like