Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

SQL - Select Statement

Back


SELECT FROM

-- Select all records
SELECT *
FROM table_name;

-- Select single field
SELECT col_name
FROM table_name;

-- Select multiple fields
SELECT col_name, col_name, ...
FROM table_name;

Alias: Column 列别名

Using AS

-- return column with the name of alias name
SELECT column_name AS alias_name
FROM table_name;

Using Space Character 使用空格

-- return column with the name of alias name
SELECT column_name alias_name
FROM table_name;

||: Concatenation operator

-- combining two columns
SELECT field_name || field_name
FROM table_name

-- concatenate two columns with a space character
SELECT field_name || ' ' || field_name
FROM table_name


Select Arithmetic

Operator Operation
+ Add
- Subtract
* Multiply
/ Divide

SELECT DISTINCT: Return unique data

SELECT DISTINCT col_name FROM tb_name;

# use parethesis to clarify the column to which the DISTINCT is being applied.
SELECT DISTINCT(col_name)  FROM tb_name;

-- Remove duplicate items by using DISTINCT
SELECT DISTINCT field_name, field_name
FROM table_name;


SELECT WHERE: Filter returned rows

SELECT column_name
    ,column_name
    ,column_name
FROM table_name
WHERE column_name operator value;

Comparison Operator 比较运算符

Operator Description
= Equality, equal to
<>,!=, ^= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
-- return a set of data where state name start later than given string "GA"
-- if it is <, it  means the state name start alphabetically earlier than the given string
SELECT firstname
    ,lastname
    ,state
FROM customers
WHERE state > 'GA';


SELECT firstname
    ,lastname
    ,state
FROM customers
WHERE state <> 'GA';


Calculation Expression

-- 注意:
-- 1. WHERE语句中的column_name列名可以是表达式;
-- 2. column_name不能用alias,下例中只能键入表达式,不能用别名profit
-- 3. value也可以是表达式。

SELECT title
    ,retail-cost AS profit
FROM books
WHERE retail-cost < cost*.2;


Logical Operators 逻辑运算符

WHERE (category = 'FAMILY LIFE')
OR (pubid = 4
AND cost > 15)





BETWEEN AND Operator

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN low_boudary AND high_boudary;

IN Operator


SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);


LIKE and ILIKE Opeartor

Wildcard Description Note
_ underscore 下划线, is used to represent exactly one character; can’t represent nothing
% percent 百分号, is used to represent any number of characters; can represent nothing
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
SELECT * FROM customer
WHERE first_name LIKE 'J%';

SELECT * FROM customer
WHERE first_name LIKE '_her%';

SELECT * FROM customer
WHERE first_name NOT LIKE '_her%';

%a  -- End with 'a'
a%  --Start with 'a'
%a% --Contain 'a'
_a_ --The string is composed of 3 characters with 'a' in the middle
_a  --The string is composed of 2 characters and ends with 'a'.
a_  --The string is composed of 2 characters and starts with 'a'.


IS NULL


ORDER BY: Sort Rows

-- Ascending order
SELECT col_1, col_2 FROM tb_name ORDER BY col_1 ASC;

-- Descending order
SELECT col_1, col_2 FROM tb_name ORDER BY col_1 DESC;

-- Multiple order, Same order
SELECT col_1, col_2 FROM tb_name ORDER BY col_1, col_2;

-- Multiple order, Different order
SELECT col_1, col_2
FROM tb_name
ORDER BY col_1 ASC, col_2 DESC;

LIMIT: Limits number of returned rows

SELECT col_1, col_2
FROM tb
WHERE condition
ORDER BY col_1
LIMIT num

Sub Query

SELECT *
FROM tb
WHERE (sub query)

EXISTS function

SELECT col
FROM tb
WHERE EXISTS(
  SELECT col
  FROM tb
  WHERE condition
)

UNION

SELECT col FROM tb_1
UNION
SELECT col FROM tb_2;

TOP