0% found this document useful (0 votes)
32 views27 pages

Lecture #6. SELECT

The document discusses the SELECT statement in SQL. It covers the FROM clause and how it specifies row sources like tables, views, functions, subqueries, and VALUES phrases. It also discusses different types of joins like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Finally, it covers other clauses like WHERE, which specifies conditional filtering, and GROUP BY, which groups rows based on column values.

Uploaded by

Tobias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views27 pages

Lecture #6. SELECT

The document discusses the SELECT statement in SQL. It covers the FROM clause and how it specifies row sources like tables, views, functions, subqueries, and VALUES phrases. It also discusses different types of joins like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Finally, it covers other clauses like WHERE, which specifies conditional filtering, and GROUP BY, which groups rows based on column values.

Uploaded by

Tobias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

SQL Language.

Part #2
SELECT
General issues
1. Use this form to confirm your presence at a lecture
2. Review of the previous lecture (SQL1)
3. Laboratory work #2
4. Сьогодні - тест
SELECT: FROM clause
Row sources are specified after the FROM keyword. This part of the query is
called the FROM clause.

You can select data from zero, one or more sources. If there is no source of
strings, then the FROM clause should not be present. The following string
sources are possible:

● Table: select * from student;


● View: create view v1 as select * from student; select * from v1;
● function: select * from getStudent(3); -- see next slides
● subquery: select * from (select name from student) as t
● VALUES phrase: select * from (VALUES (1, 2, 3), (2, null, 4)) AS t
SELECT: FROM clause
Tables as a source:

One table: select * from user; -- all rows from user

Two tables - JOIN:


SELECT - FROM - JOIN
CROSS JOIN - Cartesian product:

select * from "group" cross join student;

select * from "group", student;


JOIN Syntax
The JOIN clause can be specified in one of three ways:
using the ON, USING, and NATURAL keywords.
<first table> JOIN <second table> ON <condition>
The condition can be any SQL expression that returns a boolean value. You
don't even need to include the columns of the tables being joined.
<first table> JOIN <second table> USING (<field list>)
It is assumed here that all fields are joined by equality, separated by commas
in the <field list>. Fields must exist in both tables and be named the same, so
this syntax is not flexible enough.
<first table> NATURAL JOIN <second table>
JOIN Syntax - Examples
● select * from "group" as g1 INNER JOIN student as s on
g1."GroupID"=s."GroupID"
● select * from "group" NATURAL JOIN student
● select * from "group" JOIN student USING ("GroupID")

All the queries are equivalent.


JOIN Classification
JOIN Types
JOIN Types Examples
INNER JOIN

The same query:


select * from “group” as g1, student as s
where g1.”GroupID” = s.”GroupID”
LEFT JOIN (INCLUSIVE)
LEFT JOIN (EXCLUSIVE)
RIGHT JOIN (INCLUSIVE)
RIGHT JOIN (EXCLUSIVE)
FULL JOIN (INCLUSIVE)
FULL JOIN (EXCLUSIVE)
Self-joins
You can join a table to itself, this is called a self-join.
Self-joins (2 levels)

All paths of length 1


Self-joins (3 levels)

All paths of length 2


FROM as a Function
CREATE FUNCTION getStudent(int) RETURNS SETOF Student AS $$

SELECT * FROM Student WHERE "StudentID" = $1;

$$ LANGUAGE SQL;
FROM as a Function
“generate_series”
WHERE clause
Conditional expressions also use other operators or expressions that return
boolean values:

• comparison operators;

• pattern matching operators (LIKE and ILIKE, SIMILAR and ~ - regular expr); In
the meantime, there is no need to worry about it. ”

• operator OVERLAPS of dates: (DATE '2020-01-10', DATE '2020-02-01') OVERLAPS (DATE


'2020-01-20', DATE '2020-02-10');

• constructs for comparing strings and arrays (a IN (1, 2, 3)); ANY, ALL

• expressions of subqueries; a in (SELECT) NOT IN together with a subquery is


sometimes very slow
WHERE Examples
1. select * from student where id>10 and age<20;
2. select * from student where id in (1,2,3,4,5);
3. select * from student where id in (select age from people);
4. select * from student where id > ALL (select age from people);
5. select * from student where id < ANY (select age from people);
6. select * from student where id BETWEEN 10 AND 15; id>=10 and id<=15
7. select * from student where name like ‘A%’ or name like ‘%A%’ or name
like ‘_B’
8. select * from student where EXISTS (select * from people);
9. select * from Student where birthday < Now(); -- a function
WHERE Regular Expressions
SQL standard's definition of a regular expression:
string SIMILAR TO pattern [ESCAPE escape-character]

'abc' SIMILAR TO 'abc' true

'abc' SIMILAR TO 'a' false

'abc' SIMILAR TO '%(b|d)%' true

'abc' SIMILAR TO '(b|c)%' false


WHERE Regular Expressions
POSIX Regular Expressions

'abc' ~ 'abc' true

'abc' ~ '^a' true

'abc' ~ '(b|d)' true

'abc' ~ '^(b|c)' false

Find more here and here


GROUP BY
The GROUP BY clause is used for grouping. Grouping is splitting the entire set
of input records into several groups:

getting one resulting row for each group.

The grouping is created by the list of expressions. All rows with the same
combination of group group values are combined into one group.

Thus, groups of expression value values specified in the GROUP BY clause.

The most popular aggregate functions are COUNT, MAX, MIN, SUM, AVG. The
others are here.

You might also like