Data Analysis/SQL

[w3schools] SQL Study①

AubreyJeong 2019. 1. 22. 12:21

1. SQL
  • SQL stands for Structured Query Language.
  • SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987


2. What is RDBMS?


RDBMS stands for Relational Database Management System.

RDBMS is the basis for SQL, and for all modern database systems 

such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

The data in RDBMS is stored in database objects called tables. 

A table is a collection of related data entries and it consists of columns and rows.


Every table is broken up into smaller entities called fields. 

* A field is a column in a table that is designed to maintain specific information about every record in the table.


A row(=record) is each individual entry that exists in a table. 

  For example, there are 91 records in the above Customers table. A record is a horizontal entity in a table.

A column is a vertical entity in a table that contains all information associated with a specific field in a table.


cf. Difference between a field and a column

  -> A column is a collection of cells aligned vertically in a table. A field is an element in which one piece of information is stored, such as the Received field.



3. Database Tables


A database most often contains one or more tables. 

Each table is identified by a name (e.g. "Customers" or "Orders"). 

Tables contain records (rows) with data.



4. SQL Syntax


SQL keywords are NOT case sensitive. 


Some database systems require a semicolon at the end of each SQL statement.

A semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.


【 Some of the most important SQL commands】

  • SELECT - extracts data from a database
  • UPDATE - updates data in a database
  • DELETE - deletes data from a database
  • INSERT INTO - inserts new data into a database
  • CREATE DATABASE - creates a new database
  • ALTER DATABASE - modifies a database
  • CREATE TABLE - creates a new table
  • ALTER TABLE - modifies a table
  • DROP TABLE - deletes a table
  • CREATE INDEX - creates an index (search key)
  • DROP INDEX - deletes an index


5. The SQL SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.



SELECT DISTINCT column1, column2, ...
FROM table_name;


Q. How many different values in a selected column?

SELECT COUNT(DISTINCT Country) FROM Customers;

Note: The example above will not work in Firefox and Microsoft Edge! 
Because COUNT(DISTINCT column_name) is not supported in Microsoft Access databases. 

SELECT Count(*) AS DistinctCountries

FROM (SELECT DISTINCT Country FROM Customers); 



6. The WHERE clause


The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified condition.



SELECT column1, column2, ...
FROM table_name
WHERE condition;



cf. SQL requires single quotes around text values (most database systems will also allow double quotes).

However, numeric fields should not be enclosed in quotes:


SELECT * FROM Customers
WHERE Country='Mexico';


SELECT * FROM Customers
WHERE CustomerID=1;


Operator

Description

=

 Equal

<>  Not equal. Note: In some versions of SQL this operator may be written as !=
>  Greater than
<  Less than
>=

 Greater than or equal

<=  Less than or equal
BETWEEN  Between a certain range
LIKE  Search for a pattern
IN  To specify multiple possible values for a column





7. The AND, OR and Not Operators



The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

  • The AND operator displays a record if all the conditions separated by AND are TRUE.
  • The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.



SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;


ex.


SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');


SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';


SELECT * FROM Customers

WHERE NOT City = 'Berlin';


   SELECT * FROM Customers

WHERE City = 'Berlin' OR City = 'London';




8. The ORDER BY keyword


The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.



SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;



※ ORDER BY Several Columns Example


Q1. The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:


-> orders by the first condition(column mentioned first) and if some rows have the same value in that, it orders them by the second condition(the following one).


SELECT * FROM Customers
ORDER BY Country, CustomerName;    



Q2.The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "CustomerName" column:


SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;




'Data Analysis > SQL' 카테고리의 다른 글

데이터베이스 튜닝  (1) 2019.03.05
MS SQL nchar vs nvarchar  (0) 2019.01.29
[w3schools] SQL ③ - JOIN series  (0) 2019.01.28
[w3schools] SQL Study②  (0) 2019.01.25