Docsity

Introducción a SQL: Conceptos Básicos y Ejemplos Prácticos, Ejercicios de Principios de la Gestión de Bases de Datos

Vista previa parcial del texto

¡Descarga Introducción a SQL: Conceptos Básicos y Ejemplos Prácticos y más Ejercicios en PDF de Principios de la Gestión de Bases de Datos solo en Docsity!

SQL

Select

José R. Paramá

Contents

Introduction Connecting to Oracle with Dbeaver Tables used in the examples Previous concepts Select statement Distinct Order by Predicates Functions Group by Having Join Subqueries Query composition

Introduction

Manufacturers comply with most of it, but some divergences exist. The SQL implemented in each product is called dialect. In spite of its name, it is not just a query language, it covers:

DDL (data definition language). DML (data manipulation language).

SQL can be used for any task inside the DBMS (creating users, creating permissions, granting permissions, controlling concurrency, creating structures to store and access data, etc.)

Introduction

Other characteristics: SQL can be embedded in programs written in general-purpose languages. It is a non-procedural language (we specify what we want, not how to get it).

It does not manage sets of rows (like the theoretical relational model), it manages collections of rows (there is no order, but repeated rows may exist).

Terminology also changes: tables, rows and columns instead of relations, tuples and attributes.

Tables used in the examples

DEPT Column Type Description DEPTNO NUMBER(2) NOTNULL^ Department number or code It is the primary key DNAME VARCHAR2(14) Department name LOC VARCHAR2(13) Municipality or city where the department islocated

EMP Column Type Description EMPNO: NUMBER(4) NOTNULL^ Employee number or code. It is the primary key of the table ENAME VARCHAR2(10) Employee name JOB VARCHAR2(9) Employee job

MGR NUMBER(4) Manager’s code Foreign key that (cyclically) references table EMP

HIREDATE DATE Hire date SAL NUMBER(7, 2) Monthly salary COMM NUMBER(7, 2) Commission

DEPTNO NUMBER(2) Code of the employee’s department. Foreign keythat references table DEPT

PRO Columns Type Description PRONO NUMBER(4) NOTNULL^ Project number/code It is the primary key PNAME VARCHAR2(10) Project name LOC VARCHAR2(13) City where the project is developed DEPTNO NUMBER(2)

Code of the department that manages the project. Foreign key that references table DEPT

EMPPRO Column Type Description EMPNO NUMBER(4) NOT NULL Employee number/code Foreign key that references table EMP PRONO NUMBER(4) NOT NULL Project number/code Foreign key that references table PRO HOURS NUMBER(2) Number of hours that an employee worked in a project

A null in COMM means that the employee does not work on commission (the value is not applicable).

A null in MGR means that the employee does not have a manager (it is also "not applicable")

Primary key of the table

Tables used in the examples

Previous concepts

Null values:

A null value or NULL implies absence of information, either because the datum is

unkown or because it is not applicable.

It must be differentiated from any other value, including 0 in the case of numeric data, or the empty string in the case of character data.

Previous concepts

Expressions^1 :

An expression is the formulation of a sequence of operations, that is, a combination of operators, operands and parentheses that, when executed,

returns a single scalar value as a result.

The operands can be constants, column names, functions, other expressions and other elements.

The data type of each operand must be the same. If an operand is null, the result is also null.

Numeric operators: + - * / Alphanumeric operators: || (concatenation of two character strings) Ejemplos: • •^3 ’House’ No son expresiones:

  • 3+
  • ’A’|| ’BC’
  • ENAME
  • SAL*1.
  • 0.5 * COMM
  • SAL + COMM
  • (Select COMM FROM EMP WHERE EMPNO = 7499)
    • SAL < 1500
    • (SAL+COMM) >=

(^1) The definition of expression is restricted to the “Core SQL” version of the standard

Contents

Introduction

Previous concepts

Select statement

Distinct

Order by

Predicates

Functions

Group by

Having

Join

Subqueries

Query composition

Select statement

The SELECT statement is used to select or retrieve data from one or more tables.

It works with one or more tables in the DB and the result is another table, sometimes called result table, but this result table will not be part of the DB.

SELECT [DISTINCT|ALL] {* | [, ] ...} FROM [[INNER|LEFT|RIGHT|FULL|CROSS] JOIN …] [ WHERE <where_condition>] [ GROUP BY [,,...] [ HAVING <having_condition>] [ ORDER BY <orderby_expr1>[,..]]

Select statement

Select ‘Nombre: ’ || ename, sal*0. from emp where deptno=10;

The result after this step is

True: keep it

Select statement

Select ‘Nombre: ’ || ename, sal*0. from emp where deptno=10; False False False False False False False False False True: keep it

True: keep it False

False

The result after this step is

The name of a column can be changed.

SELECT [AS] new_name, ...

Select statement

select ename as nombre , sal salario , sal+comm as "ingresos mensuales" , hiredate "fecha contratación" from emp;

Contents

Introduction

Previous concepts

Select statement

Distinct

Order by

Predicates

Functions

Group by

Having

Join

Subqueries

Query composition