Senin, 31 Oktober 2011

Sql Query dan Betwen


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.
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:

idcustomerday_of_orderproductquantity
1Tizag2008-08-01 00:00:00.000Pen4
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'

sql - between

BETWEEN is a conditional statement found in the WHERE clause. It is used to query for table rows that meet a condition falling between a specified range of numeric values. It would be used to answer questions like, "How many orders did we receive BETWEEN July 20th and August 5th?"

SQL Select Between:

USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order BETWEEN '7/20/08' AND '8/05/08';

SQL Results:

idcustomerday_of_orderproductquantity
1Tizag2008-08-01 00:00:00.000Pen4
2Tizag2008-08-01 00:00:00.000Stapler1
5Tizag2008-07-25 00:00:00.00019" LCD Screen3
6Tizag2008-07-25 00:00:00.000HP Printer2
BETWEEN essentially combines two conditional statements into one and simplifies the querying process for you. To understand exactly what we mean, we could create another query without using the BETWEEN condition and still come up with the same results, (using AND instead).

SQL Select Between:

USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order >= '7/20/08'
AND day_of_order <= '8/05/08';

SQL Results:

idcustomerday_of_orderproductquantity
1Tizag2008-08-01 00:00:00.000Pen4
2Tizag2008-08-01 00:00:00.000Stapler1
5Tizag2008-07-25 00:00:00.00019" LCD Screen3
6Tizag2008-07-25 00:00:00.000HP Printer2
As you can see from comparing the results of these two queries, we are able to retrieve the same data, but you may find BETWEEN easier to use and less cumbersome than writing two different conditional statements. In the end, the preference is really up to the individual writing the SQL Code.
More information about these queries can be

Tidak ada komentar:

Posting Komentar