Plex SQL Queries B099751BWX
Plex SQL Queries B099751BWX
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. 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.
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
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
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
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.
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:
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");
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
}
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");
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();
}
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;
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.
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 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 | 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/