About Me

Wednesday, 18 April 2012

SQL - Queries

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.

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 SELECT query 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 - 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.

0 comments:

Post a Comment