lecture-3
lecture-3
SQL Fundamentals
Evrad KAMTCHOUM
November 6, 2024
1 Introduction
5 SQL Operators
6 Aggregation Functions
8 Conclusion
What is SQL?
SQL (Structured Query Language) is a standard language for accessing and
manipulating databases. It is used to perform tasks such as querying data,
updating data, and defining the structure of databases. SQL is a powerful
language for working with databases. Understanding its basic syntax and
common data types is essential for working effectively with databases.
Standardization (1980s):
SQL gained popularity and was adopted by various database vendors.
In 1986, SQL became an ANSI (American National Standards Institute) standard,
known as ANSI SQL-86.
Subsequent versions of the standard were released, including ANSI SQL-89 and
ANSI SQL-92, which introduced new features and enhancements.
The full syntax of SQL queries can include various clauses and options,
such as:
SELECT Statement:
SELECT column1 , column2 , . . .
FROM t a b l e n a m e
WHERE c o n d i t i o n
GROUP BY column1
HAVING c o n d i t i o n
ORDER BY column1 [ASC | DESC]
LIMIT o f f s e t , count ;
These clauses and options provide flexibility and control when querying
and manipulating data in SQL databases.
When constructing SQL queries, various clauses are used to define the
desired operations and conditions. Let’s explore the roles of some common
SQL clauses:
1 SELECT: Specifies the columns to be retrieved from the database
tables.
2 FROM: Specifies the tables from which to retrieve data.
3 WHERE: Filters the rows based on specified conditions.
4 GROUP BY: Groups the rows that have the same values into
summary rows.
5 HAVING: Filters the grouped rows based on specified conditions.
6 ORDER BY: Specifies the order in which the rows are returned.
7 LIMIT: Limits the number of rows returned by the query.
The CREATE statement is used to create new database objects. Here’s the
syntax for creating a table:
CREATE TABLE t a b l e n a m e (
column1 d a t a t y p e [ c o n s t r a i n t ] ,
column2 d a t a t y p e [ c o n s t r a i n t ] ,
...
PRIMARY KEY ( column1 ) ,
FOREIGN KEY ( column2 ) REFERENCES o t h e r t a b l e ( column ) ,
UNIQUE ( column3 ) ,
...
);
This statement creates a new table with specified columns and constraints, such
as primary keys, foreign keys, and uniqueness constraints.
The SELECT statement is used to retrieve data from one or more tables.
Here’s the syntax for a basic SELECT statement:
SELECT column1 , column2 , ...
FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement selects specific columns from a table based on the specified
conditions.
The INSERT statement is used to insert new records into a table. Here’s
the syntax for an INSERT statement:
INSERT INTO t a b l e n a m e ( column1 , column2 , . . . )
VALUES ( v a l u e 1 , v a l u e 2 , . . . ) ;
This statement adds a new record with specified values into the specified
columns of a table.
The DELETE statement is used to remove records from a table. Here’s the
syntax for a DELETE statement:
DELETE FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement deletes records from the table based on the specified
conditions.
The INSERT statement is used to insert new records into a table. Here’s
the syntax for an INSERT statement:
INSERT INTO t a b l e n a m e ( column1 , column2 , . . . )
VALUES ( v a l u e 1 , v a l u e 2 , . . . ) ;
This statement adds a new record with specified values into the specified
columns of a table.
The DELETE statement is used to remove records from a table. Here’s the
syntax for a DELETE statement:
DELETE FROM t a b l e n a m e
WHERE c o n d i t i o n ;
This statement deletes records from the table based on the specified
conditions.
We distinguish namely:
Arithmetic Operators
Comparison Operators
Logical Operators
Comparison operators are used to compare values in SQL queries. Here are
the commonly used comparison operators in SQL:
= (Equal to)
<> or ! = (Not equal to)
< (Less than)
> (Greater than)
<= (Less than or equal to)
>= (Greater than or equal to)
These operators are used in conditions to filter data based on specified
criteria.
The COUNT function is used to count the number of rows in a result set or
the number of non-null values in a column. Here’s the syntax for the
COUNT function:
SELECT COUNT( column name )
FROM t a b l e n a m e ;
This function returns the number of rows or non-null values in the
specified column.
The MIN and MAX functions are used to find the minimum and maximum
values in a column, respectively. Here’s the syntax for the MIN and MAX
functions:
SELECT MIN( column name )
FROM t a b l e n a m e ;
RETURN t o t a l s a l a r y ;
END;
$$
LANGUAGE p l p g s q l ;
This function calculates the total salary of employees in the specified department and returns
the result.
Once you’ve defined a function, you can call it from your SQL queries or
other functions just like any built-in function. Here’s how you can call the
calculate total salary function we defined earlier:
SELECT c a l c u l a t e t o t a l s a l a r y ( 1 2 3 ) AS t o t a l s a l a r y ;
This query will invoke the function with the specified department ID (123)
and return the total salary of employees in that department.
Key Takeaways
Data Definition Language (DDL) in SQL is essential for defining and
managing the structure of database objects. By using DDL
statements such as CREATE, ALTER, and DROP, users can create and
modify database objects according to their requirements.
Data Manipulation Language (DML) in SQL provides the means to
query, insert, update, and delete data in database tables. By using
DML statements such as SELECT, INSERT, UPDATE, and DELETE,
users can perform various data manipulation tasks to manage the
contents of the database.
Data Control Language (DCL) in SQL provides the means to control
access to data within the database by granting and revoking privileges
or permissions on database objects. By using DCL statements such as
GRANT and REVOKE, database administrators can manage the security
and access control of the database.
Evrad KAMTCHOUM (CCMC (UBa)) Database Systems November 6, 2024 49 / 50
Conclusion (2)
Key Takeaways
SQL operators are essential for performing various operations and
comparisons in SQL queries. By using arithmetic, comparison, and
logical operators, users can manipulate and filter data in the database
to retrieve the desired results.
Aggregation functions are powerful tools for summarizing and
analyzing data in SQL queries. By using functions such as COUNT,
SUM, AVG, MIN, and MAX, users can derive valuable insights from
database tables and compute summary statistics as needed.
Writing functions with PostgreSQL using PL/pgSQL allows you to
extend the functionality of the database and perform custom tasks
within the database environment. By encapsulating logic in functions,
you can improve code organization, reusability, and maintainability.