80 likes | 179 Views
This guide covers the essential aspects of using the SQL SELECT statement in MySQL for effective data retrieval. It explains how to select all columns or specific columns from a given table and offers insights into using arithmetic operators, comparison operators, and logical operators within WHERE clauses. Additionally, it introduces methods for restricting the output using the WHERE clause and sorting results with the ORDER BY clause. Examples and sample queries are provided for clarity, making it easier to apply the concepts practically.
E N D
MySQL SQL for MySQL (I) Salim Mail : salim.sucipto@gmail.com Phone : 0815-188-2384 YM : talim_bansal Blog : http://salimsribasuki.wordpress.com MySQL - Query 1
Summary • Retrieving Data Using the SQL SELECT Statement • Restricting and Sorting Data • Using Single-Row Functions to Customize Output MySQL - Query
SELECT COMMON PATTERN SELECT <*, Field 1,….., Field n, Aggregate Function> FROM < Table Name> WHERE <Condition> AND/OR <If any additional condition> GROUP BY <Field 1,……, Field n> HAVING <Condition> ORDER BY <Field1,…., Field n> MySQL - Query
Retrieving Data Using the SQL SELECT Statement • Select All Columns • Syntax : SELECT * FROM <Table Name>; • Sample : SELECT * FROM ti3k_item_master • Select Specific Columns • Syntax : SELECT <Column Name, Column Name, Column Name,……Column Name> • FROM <Table Name>; • Sample : SELECT item_id, item_name, item_uom • FROM ti3k_item_master; • Use Arithmetic Operators MySQL - Query
Retrieving Data Using the SQL SELECT Statement • Understand Operator Precedence • Precedence defines the order that Oracle uses when evaluating different operators in the same expression. Every operator has a predefined precedence. Oracle evaluates operators with a higher precedence before it evaluates operators with a lower precedence. Operators with equal precedence will be evaluated from left to right MySQL - Query
Restricting and Sorting Data • Write queries that contain a WHERE clause to limit the output retrieved • Syntax : Select <Column Name,……Column Name> • From <Table Name> • Where <Column Name or Logical Phrase>; • Sample : SELECT item_id, item_name, item_uom • FROM ti3k_item_master • Where item_uom ='Set'; • Write queries that contain an ORDER BY clause sort the output of a SELECT statement (Ascending or Descending) • Sample : Select * from ti3k_item_master • Order by item_uom asc; • Sample : Select * from ti3k_item_master • Order by item_id desc; MySQL - Query
Restricting and Sorting Data List the comparison operators and logical operators that are used in a WHERE clause MySQL - Query
Sample SELECT with WHERE using Comparison operators Exercise: select item_uom “UOM” from ti3k_item_master where item_code =‘DRL10’; select item_id, item_code, item_name, item_uom, remark From ti3k_item_master where item_id > 50 And item_uom =‘Each’; select * from ti3k_item_master where item_uom in (‘Sax’,’Set’) Order by item_uom, item_id; select * from ti3k_item_master where remark is null Or created_by = ‘Salim’; select * from ti3k_item_master where item_id between 10 and 20; MySQL - Query