const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
toString().padStart(width) : cell.padEnd(width); }).join(''))).join
}; const proportion = (max, val) => Math.round(val * 100 / max); co
calcProportion = table => { table.sort((row1, row2) => row2[DENSITY
row1[DENSITY_COL]); const maxDensity = table[0][DENSITY_COL]; table
forEach(row => { row.push(proportion(maxDensity, row[DENSITY_COL]))
return table; }; const getDataset = file => { const lines = fs.read
FileSync(file, 'utf8').toString().split('n'); lines.shift(); lines
return lines.map(line => line.split(',')); }; const main = compose
(getDataset, calcProportion, renderTable); const fs = require('fs'
compose = (...funcs) => x => funcs.reduce((x, fn) => fn(x), x); con
DENSITY_COL = 3; const renderTable = table => { const cellWidth = [
8, 8, 18, 6]; return table.map(row => (row.map((cell, i) => { const
= cellWidth[i]; return i ? cell.toString().padStart(width) : cell.p
(width); }).join(''))).join('n'); }; const proportion = (max, val)
Введение в SQL
(Structured Query Language)
Timur Shemsedinov
github.com/HowProgrammingWorks
github.com/tshemsedinov
Chief Technology Architect at Metarhia
Lecturer at Kiev Polytechnic Institute
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT Statement
The SELECT statement is used to query
the database and retrieve selected data that
match the criteria that you specify.
SELECT Id, Login, FullName
FROM SystemUser
WHERE Login = “Marcus”;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT Clauses
The SELECT statement has five main clauses
to choose from, although, FROM is the only
required clause. Each of the clauses have
a vast selection of options, parameters, etc.
SELECT..FROM, WHERE,
GROUP BY, HAVING, ORDER BY
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT statement format
SELECT [ALL | DISTINCT] column1[, column2]...
FROM table1[, table2]...
[WHERE <conditions>]
[GROUP BY <columns>]
[HAVING <conditions>]
[ORDER BY <columns> [ASC | DESC] ]
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT..WHERE: Comparison Operators
= Equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to (also <>)
LIKE String comparison test
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT..WHERE: LIKE %
This will select all records with Login
starts with “Mar”
SELECT Id, Login, FullName
FROM SystemUser
WHERE Login LIKE “Mar%”;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT..WHERE: LIKE _
This will select all records with Login
starts with “Mar”, ends with “us”
and contains any 4th letter in the middle
SELECT Id, Login, FullName
FROM SystemUser
WHERE Login LIKE “Mar_us”;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT DISTINCT
If you would like to retrieve just the unique
records in specified columns, you can use
the "DISTINCT" keyword. DISTINCT will discard
the duplicate records for the columns you
specified after the "SELECT" statement:
SELECT DISTINCT FullName FROM SystemUser;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT ALL
ALL will display "all" of the specified columns
including all of the duplicates. The ALL keyword
is the default if nothing is specified.
SELECT Id, Login, FullName FROM SystemUser
WHERE Id =
ALL (SELECT UserId WHERE IP = “127.0.0.1”);
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
SELECT ANY
The ANY operator is used with a WHERE or
HAVING clause. The ANY operator returns true if
any of the subquery values meet the condition.
SELECT Id, Login, FullName FROM SystemUser
WHERE Id =
ANY (SELECT UserId WHERE IP = “127.0.0.1”);
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
Aggregate Functions
MIN the smallest value in a given column
MAX the largest value in a given column
SUM the sum of the values in a given column
AVG the average value of a given column
COUNT the total number of values in a col.
COUNT(*) the number of rows in a table
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
Aggregate Functions
Aggregate functions are used to compute against
a "returned column of numeric data" from your
SELECT. They basically summarize the results of
a particular column of selected data.
Aggregate functions are required for the "GROUP
BY", these functions can be used without the
"GROUP BY" clause.
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
Aggregate Functions
SELECT avg(Salary) FROM Employee;
SELECT avg(Salary) FROM Employee
GROUP BY Department;
SELECT count(*) FROM Employee;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
GROUP BY clause
The GROUP BY clause will gather all of the rows
together that contain data in the specified
column(s) and will allow aggregate functions
to be performed on the one or more columns.
SELECT Item, sum(Price)
FROM Goods GROUP BY Supplier;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
GROUP BY clause
Following statement will select the maximum
salary for the people in each department.
SELECT max(Salary), Department
FROM Employee GROUP BY Department;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
HAVING clause
The HAVING clause allows you to specify
conditions on the rows for each group. In other
words, which rows should be selected will be
based on the conditions you specify.
SELECT column1, SUM(column2) FROM <tables>
GROUP BY <columns> HAVING <condition>;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
HAVING clause
Let's say you have an employee table with cols:
Name, Department, Salary. If you would like to
select the average salary for each Employee in
each Department if their salary is over 200:
SELECT Department, avg(Salary) FROM Employee
GROUP BY Department HAVING avg(Salary) > 200;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
ORDER BY clause
ORDER BY is an optional clause which will
allow you to display the results of your query in
a sorted order (ascending or descending)
based on the columns specified column.
SELECT column1, SUM(column2) FROM <tables>
ORDER BY <columns> [ASC | DESC];
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
ORDER BY clause
This statement will select the Id, Department,
Name, and Salary from the Info table where the
Department equals 'Sales' and will list the results
in Ascending order based on Salary:
SELECT Id, Department, Name, Salary FROM Info
WHERE Department = 'Sales' ORDER BY Salary;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
ORDER BY clause
You can order based on multiple columns.
To do so you can specify columns list:
SELECT Id, Department, Name, Salary, Age
FROM Info WHERE Department = 'Sales'
ORDER BY Salary, Age DESC;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
Combining Conditions
The AND operator can be used to join two or
more conditions in the WHERE clause.
Both sides of the AND condition must be true
for those rows to be displayed.
SELECT column1, SUM(column2) FROM <tables>
WHERE <condition1> AND <condition2>;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
Combining Conditions
The OR operator can be used to join two or more
conditions in the WHERE clause also. With the OR
operator, either side can be true or both.
SELECT Id, Name, Title, Salary FROM Info
WHERE Title = “Sales” OR
(Title = “Programmer” AND Salary >= 45000);
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
WHERE..IN condition
The IN conditional operator is really a set
membership test operator, whether or not
a value is "in" the set.
SELECT col1, SUM(col2) FROM <tables>
WHERE col3 IN (<values>);
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
WHERE..IN condition
To select all employees from listed departments:
SELECT Id, Department, Salary FROM Employee
WHERE Department IN ('Software', QA');
SELECT Id, Department, Salary FROM Employee
WHERE Department = 'Software' OR Department = QA';
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
WHERE..BETWEEN condition
The BETWEEN conditional operator is used to
check whether a value is "between" the two values
specified after BEFORE and separated AND.
You can also use NOT BETWEEN.
SELECT col1, SUM(col2) FROM <tables>
WHERE col3 BETWEEN value1 AND value2;
const fs = require('fs'); const compose = (...funcs) => x => funcs.
reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab
table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma
=> (row.map((cell, i) => { const width = cellWidth[i]; return i ? c
WHERE..BETWEEN condition
SELECT Id, Age, LastName, Salary
FROM Employes
WHERE Age BETWEEN 30 AND 40;
SELECT Id, Age, LastName, Salary
FROM Employee
WHERE Age >= 30 AND Age <= 40;

Введение в SQL

  • 1.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c toString().padStart(width) : cell.padEnd(width); }).join(''))).join }; const proportion = (max, val) => Math.round(val * 100 / max); co calcProportion = table => { table.sort((row1, row2) => row2[DENSITY row1[DENSITY_COL]); const maxDensity = table[0][DENSITY_COL]; table forEach(row => { row.push(proportion(maxDensity, row[DENSITY_COL])) return table; }; const getDataset = file => { const lines = fs.read FileSync(file, 'utf8').toString().split('n'); lines.shift(); lines return lines.map(line => line.split(',')); }; const main = compose (getDataset, calcProportion, renderTable); const fs = require('fs' compose = (...funcs) => x => funcs.reduce((x, fn) => fn(x), x); con DENSITY_COL = 3; const renderTable = table => { const cellWidth = [ 8, 8, 18, 6]; return table.map(row => (row.map((cell, i) => { const = cellWidth[i]; return i ? cell.toString().padStart(width) : cell.p (width); }).join(''))).join('n'); }; const proportion = (max, val) Введение в SQL (Structured Query Language) Timur Shemsedinov github.com/HowProgrammingWorks github.com/tshemsedinov Chief Technology Architect at Metarhia Lecturer at Kiev Polytechnic Institute
  • 2.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT Statement The SELECT statement is used to query the database and retrieve selected data that match the criteria that you specify. SELECT Id, Login, FullName FROM SystemUser WHERE Login = “Marcus”;
  • 3.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT Clauses The SELECT statement has five main clauses to choose from, although, FROM is the only required clause. Each of the clauses have a vast selection of options, parameters, etc. SELECT..FROM, WHERE, GROUP BY, HAVING, ORDER BY
  • 4.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT statement format SELECT [ALL | DISTINCT] column1[, column2]... FROM table1[, table2]... [WHERE <conditions>] [GROUP BY <columns>] [HAVING <conditions>] [ORDER BY <columns> [ASC | DESC] ]
  • 5.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT..WHERE: Comparison Operators = Equal > Greater than < Less than >= Greater than or equal to <= Less than or equal to != Not equal to (also <>) LIKE String comparison test
  • 6.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT..WHERE: LIKE % This will select all records with Login starts with “Mar” SELECT Id, Login, FullName FROM SystemUser WHERE Login LIKE “Mar%”;
  • 7.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT..WHERE: LIKE _ This will select all records with Login starts with “Mar”, ends with “us” and contains any 4th letter in the middle SELECT Id, Login, FullName FROM SystemUser WHERE Login LIKE “Mar_us”;
  • 8.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT DISTINCT If you would like to retrieve just the unique records in specified columns, you can use the "DISTINCT" keyword. DISTINCT will discard the duplicate records for the columns you specified after the "SELECT" statement: SELECT DISTINCT FullName FROM SystemUser;
  • 9.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT ALL ALL will display "all" of the specified columns including all of the duplicates. The ALL keyword is the default if nothing is specified. SELECT Id, Login, FullName FROM SystemUser WHERE Id = ALL (SELECT UserId WHERE IP = “127.0.0.1”);
  • 10.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c SELECT ANY The ANY operator is used with a WHERE or HAVING clause. The ANY operator returns true if any of the subquery values meet the condition. SELECT Id, Login, FullName FROM SystemUser WHERE Id = ANY (SELECT UserId WHERE IP = “127.0.0.1”);
  • 11.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c Aggregate Functions MIN the smallest value in a given column MAX the largest value in a given column SUM the sum of the values in a given column AVG the average value of a given column COUNT the total number of values in a col. COUNT(*) the number of rows in a table
  • 12.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c Aggregate Functions Aggregate functions are used to compute against a "returned column of numeric data" from your SELECT. They basically summarize the results of a particular column of selected data. Aggregate functions are required for the "GROUP BY", these functions can be used without the "GROUP BY" clause.
  • 13.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c Aggregate Functions SELECT avg(Salary) FROM Employee; SELECT avg(Salary) FROM Employee GROUP BY Department; SELECT count(*) FROM Employee;
  • 14.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c GROUP BY clause The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns. SELECT Item, sum(Price) FROM Goods GROUP BY Supplier;
  • 15.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c GROUP BY clause Following statement will select the maximum salary for the people in each department. SELECT max(Salary), Department FROM Employee GROUP BY Department;
  • 16.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c HAVING clause The HAVING clause allows you to specify conditions on the rows for each group. In other words, which rows should be selected will be based on the conditions you specify. SELECT column1, SUM(column2) FROM <tables> GROUP BY <columns> HAVING <condition>;
  • 17.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c HAVING clause Let's say you have an employee table with cols: Name, Department, Salary. If you would like to select the average salary for each Employee in each Department if their salary is over 200: SELECT Department, avg(Salary) FROM Employee GROUP BY Department HAVING avg(Salary) > 200;
  • 18.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c ORDER BY clause ORDER BY is an optional clause which will allow you to display the results of your query in a sorted order (ascending or descending) based on the columns specified column. SELECT column1, SUM(column2) FROM <tables> ORDER BY <columns> [ASC | DESC];
  • 19.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c ORDER BY clause This statement will select the Id, Department, Name, and Salary from the Info table where the Department equals 'Sales' and will list the results in Ascending order based on Salary: SELECT Id, Department, Name, Salary FROM Info WHERE Department = 'Sales' ORDER BY Salary;
  • 20.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c ORDER BY clause You can order based on multiple columns. To do so you can specify columns list: SELECT Id, Department, Name, Salary, Age FROM Info WHERE Department = 'Sales' ORDER BY Salary, Age DESC;
  • 21.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c Combining Conditions The AND operator can be used to join two or more conditions in the WHERE clause. Both sides of the AND condition must be true for those rows to be displayed. SELECT column1, SUM(column2) FROM <tables> WHERE <condition1> AND <condition2>;
  • 22.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c Combining Conditions The OR operator can be used to join two or more conditions in the WHERE clause also. With the OR operator, either side can be true or both. SELECT Id, Name, Title, Salary FROM Info WHERE Title = “Sales” OR (Title = “Programmer” AND Salary >= 45000);
  • 23.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c WHERE..IN condition The IN conditional operator is really a set membership test operator, whether or not a value is "in" the set. SELECT col1, SUM(col2) FROM <tables> WHERE col3 IN (<values>);
  • 24.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c WHERE..IN condition To select all employees from listed departments: SELECT Id, Department, Salary FROM Employee WHERE Department IN ('Software', QA'); SELECT Id, Department, Salary FROM Employee WHERE Department = 'Software' OR Department = QA';
  • 25.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c WHERE..BETWEEN condition The BETWEEN conditional operator is used to check whether a value is "between" the two values specified after BEFORE and separated AND. You can also use NOT BETWEEN. SELECT col1, SUM(col2) FROM <tables> WHERE col3 BETWEEN value1 AND value2;
  • 26.
    const fs =require('fs'); const compose = (...funcs) => x => funcs. reduce((x, fn) => fn(x), x); const DENSITY_COL = 3; const renderTab table => { const cellWidth = [18, 10, 8, 8, 18, 6]; return table.ma => (row.map((cell, i) => { const width = cellWidth[i]; return i ? c WHERE..BETWEEN condition SELECT Id, Age, LastName, Salary FROM Employes WHERE Age BETWEEN 30 AND 40; SELECT Id, Age, LastName, Salary FROM Employee WHERE Age >= 30 AND Age <= 40;