0% found this document useful (0 votes)
64 views

SQL - Queries: SELECT Queries Are The Most Commonly Used SQL

The document discusses SQL queries and provides examples of different types of SQL queries like SELECT, INSERT, UPDATE, and DELETE queries. It explains key clauses used in queries like WHERE, AND, OR, AS. The SELECT query is used to fetch/select data from database tables. The WHERE clause filters rows selected and allows conditional filtering based on column values. Multiple conditions can be used together in a WHERE clause joined by AND/OR. The AS clause temporarily renames column headings in the query results.

Uploaded by

Gayatri Diwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

SQL - Queries: SELECT Queries Are The Most Commonly Used SQL

The document discusses SQL queries and provides examples of different types of SQL queries like SELECT, INSERT, UPDATE, and DELETE queries. It explains key clauses used in queries like WHERE, AND, OR, AS. The SELECT query is used to fetch/select data from database tables. The WHERE clause filters rows selected and allows conditional filtering based on column values. Multiple conditions can be used together in a WHERE clause joined by AND/OR. The AS clause temporarily renames column headings in the query results.

Uploaded by

Gayatri Diwan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

sql - queries

SQL coins the term query as the name for its


commands. Basically, all SQL code is written in the form
of a query statement and then executed against a
database. All SQL queries perform some type of data
operation such as selecting data, inserting/updating data,
or creating data objects such as SQL databases and SQL
tables. Each query statement begins with a clause such
as SELECT,UPDATE, CREATE or DELETE.
Advertise on Tizag.com
SELECT queries are the most commonly used SQL
commands, so let's take a look at a SELECT query that
will return records from the orders table that we created
previously in the SQL Tables lesson.
SQL Query Code:
USE mydatabase;

SELECT * FROM orders;

SQL Query Results:


id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4

We'll explain the mechanics of this code in the next


lesson. For now, just know that SELECT queries
essentially tell SQL to go and "fetch" table data for your
viewing pleasure.
Here's a look at a few different query types including
a INSERT and SELECTquery we will be covering in the
next lesson, SQL Select.
SQL Query Examples:
-- Inserts data into a SQL Database/Table
INSERT INTO orders
(customer,day_of_order,product, quantity)
VALUES('Tizag','8/1/08','Pen',4);

-- Selects data from a SQL Database/Table


SELECT * FROM orders;

-- Updates data in a Database/Table


UPDATE orders SET quantity = '6'
WHERE id = '1'

More information about these queries can be found at


the following links: SQL Insert, SQL Update, SQL Delete
sql - query structure review
Structurally, each SQL query we have seen in this
lesson are similar. Each start with a clause telling SQL
which operation to perform and the remaining lines
provide more detailed information as to how we want SQL
to go about performing each SQL Command.
SQL Select Query Template:
SELECT table_column1, table_column2,
table_column3
FROM my_table;

Select queries require two essential parts. The first part


is the "WHAT", which determines what we want SQL to go
and fetch. The second part of any SELECTcommand is
the "FROM WHERE". It identifies where to fetch the data
from, which may be from a SQL table, a SQL view, or
some other SQL data object.
Now we would like SQL to go and fetch some data for
us from the orders table that was created in the previous
lesson. How do we translate this request into SQL code so
that the database application does all the work for us?
Simple! We just need to tell SQL what we want to select
and from where to select the data, by following the
schema outlined below.
SQL Select Query Code:
USE mydatabase;

SELECT id, customer, day_of_order, product,


quantity
FROM orders;

SQL Orders Table Results:


id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4
Below, we will manipulate the result output by
rearranging the list of table column names inside of
the SELECT statement.
SQL Select Query: Rearranged:
USE mydatabase;

SELECT day_of_order, customer, product, quantity


FROM orders;

SQL Orders Table Results:


day_of_order customer product quantity
2008-08-01 00:00:00.000 Tizag Pen 4

By rearranging the table column list inside


the SELECT statement, we altered the appearance of the
result set. Also, by not including the id column in the list of
table columns, SQL did not fetch any column data for this
column because we didn't ask SQL to do so.
sql - select all (*)
"SELECT (*)" is a shortcut that can be used to select all
table columns rather than listing each of them by name.
Unfortunately, going this route doesn't allow for you to
alter the presentation of the results.
SQL Select All Query:
USE mydatabase;
SELECT *
FROM orders;

SQL Orders Table Results:


id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4
sql - selecting data
The (*) query statement should be used with caution.
Using this against our little tutorial database will surely do
no harm, but using this query against an extremely large
database may not be the best practice. Large databases
may have web services or applications attached to them,
so frequently updating and accessing large quantities data
may temporarily lock a table for fractions of a second or
more. If this disruption happens to occur just as some
piece of data is being updated, you may experience data
corruption.
Taking every precaution to avoid data corruption is in
your best interest as a new SQL programmer. Corrupted
data may be lost and never recovered, and it can lead to
even more corruption inside a database. The best habits
are to be as precise as possible, and in the case of select
statements, this often means selecting minimal amounts of
data when possible.
At this point, you should feel comfortable
with SELECT and how to look into your database and see
actual data rows residing inside of tables. This knowledge
will prove invaluable as your SQL skills develop beyond
the basics and as you begin to tackle larger, more
advanced SQL projects.
sql - where
The WHERE clause sets a conditional statement, and
it can be used with any type of SQL query. As the select
query executes, SQL processes one row at a time. Each
time the conditional statement is met (returns true), a
row is returned as a result. SQL WHERE is essentially, a
filtering mechanism for SQL queries and is a tremendous
asset to any aspiring SQL developer.
Advertise on Tizag.com
SQL Where Query:
USE mydatabase;

SELECT *
FROM orders
WHERE customer = 'Tizag'

As we take a look at the results, notice how only the


rows that meet the criteria (where the customer column
value is Tizag) are returned. In this example, we are using
the WHERE clause to filter out rows and only selecting
data that meets the conditional statement.
SQL Results:
id customer day_of_order product quantity
2008-08-01
1 Tizag Pen 4
00:00:00.000
2008-08-01
2 Tizag Stapler 1
00:00:00.000
2008-07-25
5 Tizag 19" LCD Screen 3
00:00:00.000
2008-07-25
6 Tizag HP Printer 2
00:00:00.000

Conditional statements are not unique to SQL, and


neither are operators. Operators are symbols such as (=)
or (<), and they are seen inside of conditional statements
and expressions in SQL and other programming
languages. While we're not going to dive into much detail
about the different kinds of operators yet, it is a good idea
to be familiar with them and be able to recognize them
inside of conditional statements as we look over the next
few examples.
sql - where queries
With the WHERE clause on our tool belts, we can be
more creative when querying for table rows. For instance,
there may come a time where we would like to take a look
at all the orders placed after a certain date.
SQL Where Date Query:
USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order > '7/31/08'
This conditional statement will return only the orders
that have made it into the table since the end of July,
filtering out any orders in the table made prior to July 31st.
SQL Results:
id customer day_of_order product quantity
2008-08-01
1 Tizag Pen 4
00:00:00.000
2008-08-01
2 Tizag Stapler 1
00:00:00.000
2008-08-16 Hanging
3 A+Maintenance 12
00:00:00.000 Files
2008-08-15 19" LCD
4 Gerald Garner 3
00:00:00.000 Screen

Notice how the date value is formatted inside the


conditional statement. We passed a value formatted
MM/DD/YY, and we've completely neglected the hours,
minutes, and seconds values, yet SQL is intelligent
enough to understand this. Therefore, our query is
successfully executed.
sql - where with multiple conditionals
A WHERE statement can accept multiple conditional
statements. What this means is that we are able to select
rows meeting two different conditions at the same time.
Perhaps the easiest way to go about this is to add
another condition to the previous example, where we
retrieved only the orders placed after July 31st. We can
take this example one step further and link two conditional
statements together with "AND".
SQL Where And:
USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order > '7/31/08'
AND customer = 'Tizag'

At this point, we have sent SQL two conditional


statements with a singleWHERE clause, essentially
applying two filters to the expected result set.
SQL Results:
id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4
2 Tizag 2008-08-01 00:00:00.000 Stapler 1

By applying the AND clause, SQL has now been asked


to return only rows that meet both conditional statements.
In this case, we would like to return all orders that were
made before July 31st and made by a specific company -
which is, in this case, Tizag. We have more examples
of SQL AND/OR. Just follow the link.
sql - as
SQL AS temporarily assigns a table column a new
name. This grants the SQL developer the ability to make
adjustments to the presentation of query results and allow
the developer to label results more accurately without
permanently renaming table columns.
Advertise on Tizag.com
SQL Select As Code:
USE mydatabase;

SELECT day_of_order AS "Date",


customer As "Client",
product,
quantity
FROM orders;

SQL Orders Table Results:


Date Client product quantity
2008-08-01 00:00:00.000 Tizag Pen 4

SQL AS allows us to use any name at the presentation


level and helps the developer better describe a column in
the result set.
SQL Select Arithmetic Query:
USE mydatabase;

SELECT (5 + 12) AS "5 plus 12 is"

SQL Arithmetic Results:


5 plus 12 is
17
sql - operators
SQL operators are found in just about every SQL
query. Operators are the mathematical and equality
symbols used to compare, evaluate, or calculate values.
Equality operators include the (<), (>), and (=) symbols,
which are used to compare one value against another.
Each of these characters have special meaning, and when
SQL comes across them, they help tell SQL how to
evaluate an expression or conditional statement. Most
operators will appear inside of conditional statements in
the WHERE clause of SQL Commands.
Advertise on Tizag.com
Operators come in three flavors: mathematical, logical,
and equality. Mathematical operators add, subtract,
multiply, and divide numbers. Logical operators
include AND and OR. Take note of the following tables for
future reference.
SQL operators are generally found inside of queries--
more specifically, in the conditional statements of
the WHERE clause.
SQL Equality Operator Query:
USE mydatabase;

SELECT customer,day_of_order
FROM orders
WHERE day_of_order > '7/31/08'
Sql Equality Operator:
customer day_of_order
Tizag 2008-08-01 00:00:00.000
Tizag 2008-08-01 00:00:00.000

In this case, we've used the equality operator greater


than (>) to return orders from the orders table with a date
greater than '7/31/08'.
sql - equality operator table
Equality involves comparing two values. To do so
requires the use of the (<), (>), or (=) special characters.
Does X = Y? Is Y < X? These are both questions that can
be answered using a SQL Equality Operator expression.
SQL Equality Operators:
Operator Example Defined Result
=, IS 5=5 5 equal to 5? True
!=, IS NOT 7 != 2 7 IS NOT (!=) equal to 2? True
< 7<4 7 less than 4? False
> 7>4 greater than 4? True
<= 7 <= 11 Is 7 less than or equal to 11? True
>= 7 >= 11 Is 7 greater than or equal to 11? False
sql - mathematical operators
SQL mathematical operations are performed using
mathematical operators (+, -, *, /, and %). We can use
SQL like a calculator to get a feel for how these operators
work.
SQL Mathematical Operators:
SELECT
15 + 4, --Addition
15 - 4, --Subtraction
15 * 4, --Multiplication
15 / 5, -- Division
15 % 4; --Modulus

SQL Results:
Addition Subtraction Multiplication Division Modulus
19 11 60 3 3

Modulus may be the only unfamiliar term on the chart.


Modulus performs division, dividing the first digit by the
second digit, but instead of returning a quotient, a
"remainder" value is returned instead.
Modulus Example:
USE mydatabase;

SELECT (5 / 2) -- = 2.5
SELECT (5 % 2) -- = 1 is the value that will be
returned

sql - logical operators


These operators provide you with a way to specify
exactly what you want SQL to go and fetch, and you may
be as specific as you'd like! We'll discuss these a little later
on and provide some real world scenarios as well.
We cover these operators thoroughly in the SQL
AND/OR lesson.
 AND - Compares/Associates two values or
expressions
 OR - Compares/Associates two values or expressions

You might also like