Basic SQL Grammar
04 Nov 2018SQL (to be Updated)
- Structured Query Language (SQL), is a programming language used to manage or process data in Relational Database Management System (RDBMS)
- SQL is useful in handling structured data that includes relations between different data tables
- SQL is used to create, manage, query, manipulate the data with statements
img source: https://medium.com/@swiftsnippets/database-system-s-rdbms-and-nosql-6afeef8168e5
Basic Statements
SELECT
SELECT is used to select data from a database (most statements are self-explanatory…)
SELECT column1, column2, … FROM table_name;
If you want to select all columns, simply use * (asterisk)
SELECT * FROM table_name;
If you want to select only unique or different values, use DISTINCT
SELECT DISTINCT column1, column2, … FROM table_name;
If you want to select values with certain conditions, use WHERE
SELECT column1, column2, … FROM table_name WHERE condition;
The following are operations that can be used with the WHERE clause:
Operator | Description |
---|---|
= | equal |
<> or != | not equal |
< | less than |
> | greater than |
BETWEEN | between a range |
LIKE | pattern search |
IN | specify multiple values as an inclusive syntax |
Example
SELECT DISTINCT * FROM product_info WHERE price > 40;
AND, OR, NOT
AND, OR, and NOT operators are combined with other statements to filter records in a more desirably way. It is used in a similar manner just as in other programming languages.
- AND displays a record if all the conditions stated and connected with AND are TRUE
- OR displays a record if any conditions stated with OR are TRUE
- NOT displays a record if the conditions is NOT TRUE
Examples
SELECT * FROM product_info WHERE price BETWEEN 20 AND 30
SELECT * FROM product_info WHERE color = “red” OR “blue”