1. Understand SQL
It's easiest to think of the database as a filing cabinet . This filing cabinet is a physical location where data is stored .
When you put materials in a filing cabinet, you create files in the filing cabinet, and then put related materials into specific files . Such files are called tables .
Table: A structured list of a particular type of data .
The data stored in the table is the same type of data or list .
Schema: Information about the layout and characteristics of databases and tables .
A table consists of columns .
Column: A field in a table. All tables are composed of one or more columns .
Each column in the database has a corresponding data type .
Data Type: The type of data allowed. Each table column has a corresponding data type that restricts (or allows) the data stored in that column .
The data in the table is stored in rows, and each record saved is stored in its own row .
Row: A record in a table .
Each row in the table should have a column (or columns) that uniquely identifies itself.
Primary key: A column (or set of columns) whose value uniquely identifies each row in a table .
2. Retrieve data
1) Retrieve a single column
To retrieve table data using SELECT, at least two pieces of information must be given—what to select, and where to select from .
SELECT prod_name FROM Products;
The above statement uses a SELECT statement to retrieve a column named pro_name from the Products table.
2) Retrieve multiple columns
SELECT prod_id,prod_name,prod_price FROM Products;
3) Retrieve all columns
SELECT * FROM Products;
4) Retrieve distinct values
Use the DISTINCT keyword, which instructs the database to only return distinct values
SELECT DISTINCT vend_id FROM Products;
5) Limit results
SELECT prod_name FROM Products LIMIT 5;
The above code uses a SELECT statement to retrieve a single column of data. LIMIT 5 instructs a DBMS such as MySQL to return no more than 5 rows of data.
To get the next 5 rows of data, you need to specify where to start and how many rows to retrieve:
SELECT prod_name FROM Products LIMIT 5 OFFSET 5;
LIMIT 5 OFFSET 5 instructs a DBMS such as MySQL to return 5 rows of data starting from row 5. The first number is the number of rows retrieved, and the second is where to start.
Note: The first row retrieved is row 0, not row 1. So LIMIT 1 OFFSET 1 retrieves row 2, not row 1 .