/ W3SCHOOLS

W3schools - SQL_SELECT / WHERE

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

SELECT

Is used to select data from a database

The data returned is stored in a result table, called the result-set

Syntax

SELECT colum1, column2,
FROM table_name;
-- if you want to select all the fields available in the table
SELECT * FROM table_name;

SELECT DISTINCT

Is used to return only distinct values

Syntax

SELECT DISTINCT column1, column2,
FROM table_name;
-- Can count, It is not supported in MS Access
SELECT COUNT(DISTINCT column1,column2,) FROM table_name;
-- Is the workaround for MS Access
SELECT Count(*) AS Distinctcolumn1
FROM (SELECT DISTINCT column1 FROM table_name);

WHERE

Is used filter records

Is used to extract only those records that fulfill a specified condition

Syntax

SELECT column1, column2,
FROM table_name
WHERE condition;

SQL requires single quotes around text values

Numeric fields should not be enclosed in quotes

Operators in WHERE

WHERE clause can be combined with AND, OR, NOT operators

Syntax

SELECT column1, column2,
FROM table_name
WHERE condition1 AND (condition2 OR condition3);
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
< > Not equal, is some versions of SQL this operator may be !=
BETWEEN ‘A’ AND ‘B’ Between a certain range
LIKE ‘pattern’ Search for a pattern
IN(value1,value2) To specify multiple possible values for a column

If you omit the WHERE clause, all records in the table will be apply

ORDER BY

Is used to sort the result-set in ascending or descending order

It sorts the records in ascending order by default

To sort the records in descending order, use the DESC keyword

Syntax

SELECT column1, column2,
FROM table_name
ORDER BY column1, column2,ASC|DESC;
-- Can use both, ORDER BY column1 ASC, column2 DESC;