The most commonly used SQL command is SELECT statement. The SQL SELECT statement is used to query or retrieve data from a table in the database. A query may retrieve information from specified columns or from all of the columns in the table. To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name. The whole query is called SQL SELECT Statement.
Syntax of SQL SELECT Statement:
SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
table-name is the name of the table from which the information is retrieved.
SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
table-name is the name of the table from which the information is retrieved.
· column_list includes one or more columns from which data is retrieved.
· The code within the brackets is optional.
· The SELECT statement allows you to retrieve records from one or more tables in your database.
· The syntax for the SELECT statement is:
· SELECT columns
FROM tables
WHERE predicates;
FROM tables
WHERE predicates;
· Example #1
· Let's take a look at how to select all fields from a table.
· SELECT *
FROM suppliers
WHERE city = 'Newark';
FROM suppliers
WHERE city = 'Newark';
· In our example, we've used * to signify that we wish to view all fields from the suppliers table where the supplier resides in Newark.
· Example #2
· You can also choose to select individual fields as opposed to all fields in the table.
· For example:
· SELECT name, city, state
FROM suppliers
WHERE supplier_id > 1000;
FROM suppliers
WHERE supplier_id > 1000;
· This select statement would return all name, city, and state values from the suppliers table where the supplier_id value is greater than 1000.
· Example #3
· You can also use the select statement to retrieve fields from multiple tables.
· SELECT orders.order_id, suppliers.name
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
The result set would display the order_id and suppier name fields where the supplier_id value existed in both the suppliers and orders tableFROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
No comments:
Post a Comment