Background I

  • The SQL language may be considered one of the major reasons for the success of relational databases in the commercial world.

  • The name SQL is derived from Structured Query Language. Originally, SQL was called SEQUEL (for Structured English QUERY Language) and was designed and implemented at IBM Research as the interface for an experimental relational database system called SYSTEM R

  • SQL is now the standard language for commercial relational DBMSs.

Background II

  • SQL is a comprehensive database language: It has statements for data definition, query, and update

  • It also has rules for embedding SQL statements into a general-purpose programming language such as Java

Background III

SQL functions fit into two broad categories:

  • Data definition language:

    • Create database objects, such as tables, indexes, and views

    • Define access rights to those database objects

  • Data manipulation language:

    • Includes commands to insert, update, delete, and retrieve data within database tables

Data Manipulation Language

  • Adding table rows
  • Saving table changes
  • Listing table rows
  • Updating table rows
  • Restoring table contents
  • Deleting table rows
  • Inserting table rows with a select subquery

Logic of Relational Databases I

  • Relational database consists of several related tables

  • The set of multilevel related databases is more practical for data storage and using it

Logic of Relational Databases II

Relational

Logic of Relational Databases II

Hierarchical

Logic of the SQL and syntax

  1. Creation of empty table

  2. Creation of variables and setting on its parameters (type, length, null values)

  3. Filling the table with values of rows and columns

Logic of the SQL and syntax: Types of variables

Logic of the SQL and syntax

Logic of the SQL and syntax

Logic of the SQL and syntax

Exercises

Exercise 1

Task 1. Create a data table which would contain information on users’ Id, profile creation date, total amount of upvotes, age, and location.

Task 2. Choose only the first 5000 profiles. Find those users who live in Canada.

Task 3. Sort the data table you got by users’ Ids.

Exercise 2

Find titles of questions viewed more than 1000 times from site “SciFi and Fantasy”. Order questions from the most viewed to the least viewed.

Exercises

Exercise 3

Create a data table which would contain information on:

  • the number of all forum posts

  • total score of these posts

  • average amount of comments per one post

  • number of likes of the most popular post

  • number of views of the least popular post

Exercises

Exercise 4

Count average reputation for users older than 30 but younger than 45 and order ages in ascending order

Exercise 5

Find the unique profile names of people who both comment and post using a tag ‘sql’

Exercise 6

Find posts about English language written by users under 20. Choose only those posts which have title and have more posts than 0.