markdown 来自DataCamp的“数据科学SQL简介”课程的说明

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 来自DataCamp的“数据科学SQL简介”课程的说明相关的知识,希望对你有一定的参考价值。

Get a list of distinct values for a column in a table

```SQL
SELECT DISTINCT column FROM table;
```

Get the count of rows in a table

```SQL
SELECT COUNT(*) FROM table;
```
Get the count of non-null records for a column in a table

```SQL
SELECT COUNT(column) FROM table;
```

Get the count of distinct non-null records for a column in a table

```SQL
SELECT COUNT(DISTINCT column) FROM table;
```

When using the `WHERE` clause on strings in PostgreSQL, you must use single-quotes around the string.

Get values within a range (example, the 1990s)

Method 1

```SQL
SELECT *
FROM table
WHERE column >= 1990
AND column <= 1999;
```

Method 2

```SQL
SELECT *
FROM table
WHERE column
BETWEEN 1990 AND 1999;
```
Aggregate functions are useful for returning...aggregated values of a column

```SQL
SELECT AVG(column) FROM table;

SELECT SUM(column) FROM table;

SELECT MIN(column) FROM table;

SELECT MAX(column) FROM table;
```

You can group results based on values in a particular column. That column needs to be included in the `SELECT` statement. You can also group by multiple columns

```SQL
SELECT column1, column2, SUM(column3)
FROM table
GROUP BY column1, column2
```

You can't use aggregate functions in a `WHERE` clause, but you can mimic the effect using a `HAVING` clause.

`INNER JOIN` syntax

```SQL
SELECT *
FROM left_table
INNER JOIN right_table
ON left_table.id = right_table.id;
```
When dealing with multiple tables, if tables have identical column names,you have to specify the table and the column (`table1.column`) in your `SELECT` statement

When joining more than two tables you may need to use `AND` in the `ON` clause to ensure you're joining the third/nth table to the preceding join

If join fields in tables are identical, you can use `USING (key_field)` instead of, for example `ON table1.id = table2.id`

Self-join allows you to join within a single table. This is particularly helpful for comparing records within the same field

The following example allows us to compare records for a country for 2010 and 2015 and create a new field with values expressing the percentage growth rate:

```SQL
SELECT p1.country_code, 
       p1.size AS size2010,
       p2.size AS size2015,
       (p2.size - p1.size) / p1.size * 100.0 AS growth_perc
FROM populations AS p1
INNER JOIN populations AS p2
ON  p1.country_code = p2.country_code AND p1.year = p2.year - 5;
```

`CASE WHEN...THEN...ELSE...END` allows you to set column values with something akin to `IF...THEN` syntax

```SQL
CASE WHEN condition1
  THEN 'some_value1'
WHEN condition2
  THEN 'some_value2'
ELSE 'some_value3' END
AS new_column
```

以上是关于markdown 来自DataCamp的“数据科学SQL简介”课程的说明的主要内容,如果未能解决你的问题,请参考以下文章

python DataCamp:Python数据科学简介https://www.datacamp.com/courses/intro-to-python-for-data-science

python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part

数据科学中R VS Python:获胜者是...

数据科学中R VS Python:获胜者是...

人工智能第三课:数据科学中的Python

终于盼到了,Python 数据科学速查表中文版来了