Introduction To SQL
Introduction To SQL
Case Sensitivity
The most important point to be noted here is that SQL is case insensitive, which
means SELECT and Select have same meaning in SQL statements. Whereas, MySQL
makes difference in table names. So, if you are working with MySQL, then you need to
give table names as they exist in the database.
SQL CREATE DATABASE Statement
Example:
CREATE DATABASE sampleDB;
SQL USE Statement
Example:
Use sampleDB;
SQL DROP DATABASE Statement
Example:
Drop database sampleDB;
SQL CREATE TABLE Statement
With constraints
The following code block is an example, which creates a CUSTOMERS table given above,
with an ID as a primary key and NOT NULL are the constraints showing that these fields
cannot be NULL while creating records in this table −
SQL DESC Statement
In order to retrieve the result-sets of the stored data from a database table, we use the
SELECT statement.
To retrieve the data from CUSTOMERS table, we use the SELECT statement as shown
below.
Example:
SELECT * FROM CUSTOMERS;
SQL UPDATE Statement
Update example:
SQL DELETE Statement
Example:
SQL DROP TABLE Statement
Example:
DROP TABLE CUSTOMERS;
SQL TRUNCATE TABLE Statement
The TRUNCATE TABLE statement is implemented in SQL to delete the data of the table
but not the table itself.
When this SQL statement is used, the table stays in the database like an empty table.
Following is the syntax −
Example:
TRUNCATE TABLE CUSTOMERS;
SQL ALTER TABLE Statement
Example:
SQL DISTINCT Clause
SQL DISTINCT Clause
The DISTINCT clause in a database is used to identify the non-duplicate data from a
column. Using the SELECT DISTINCT statement, you can retrieve distinct values from a
column.
Following is the syntax −
Example:
SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY.
As an example, lets use the DISTINCT keyword with a SELECT query.
The repetitive salary 2000.00 will only be retrieved once and the other record is ignored.
SQL WHERE Clause
The following query is an example to fetch all the records from CUSTOMERS table where
the salary is greater than 2000, using the SELECT statement.
SQL AND/OR Operators
The AND/OR Operators are used to apply multiple conditions in the WHERE clause.
Following is the syntax −
Example:
SQL BETWEEN Clause
Example:
SQL ORDER BY Clause
The ORDER BY Clause is used to arrange the column values in a given/specified order.
Syntax:
Example:
In the following example we are trying to sort the result in an ascending order by the
alphabetical order of customer names −
In the above example we are trying to sort the result in an ascending order by the
alphabetical order of customer names.
SQL GROUP BY Clause
We are trying to group the customers by their age and calculate the average salary for each
age group using the following query −
Count()
Example:
Example
SELECT COUNT(country)
FROM Customers
WHERE country = 'UK';
Count()
Count(*)
SELECT COUNT(*)
FROM Customers;
Example:
COUNT() With DISTINCT
If we need to count the number of unique rows, we can use the COUNT() function with the
DISTINCT clause.
Count() with distinct
Group by()
The COUNT() function can be used with the GROUP BY clause to count the rows with
similar values.
Max()
SELECT query is used to retrieve data from a table. It is the most used SQL query.
We can retrieve complete table data, or partial by specifying conditions using the WHERE
clause.
Syntax of SELECT query
SELECT
column_name1, column_name2, ... column_nameN
FROM table_name;
Performing Simple Calculations using SELECT Query
SQL Arithmetic Operators are used to perform mathematical operations on the numerical
values.
SQL provides following operators to perform mathematical operations.
We can combine AND and OR operators in the below manner to write complex queries.
Syntax: SELECT * FROM table_name WHERE condition1 AND (condition2 OR
condition3);
Example:
SELECT * FROM Student WHERE Age = 18 AND (NAME = 'Ramya' OR NAME =
'Hamza');
Wildcard operators
Wildcard operators are used with the LIKE operator, which is generally used to search the
data in the database and there are four basic operators %, _,-,[range_of_chracters].
Let us explain all these operators in brief –
Operator Description
To fetch records from the Customer table with LastName not containing letters ‘y’, or ‘z’.
Query
SELECT * FROM Students WHERE LastName NOT LIKE '%[y-z]%';
concatenation operator
concatenation operator is use to link columns or character strings. We can also use
a literal. A literal is a character, number or date that is included in the SELECT
statement.
Example:
SELECT id, first_name, last_name, first_name || last_name,
salary, first_name || salary FROM myTable
Note: Here above we have used || which is known as Concatenation operator which is
used to link 2 or as many columns as you want in your select query and it is independent
of the datatype of column.
Here above we have linked 2 columns i.e, first_name+last_name as well
as first_name+salary
Example:
1) Projection
2) Selection
3) Union
4) Cross product
5)Rename
6) set difference.
Basic operations:
• select, project, and rename operations are called unary operations, because they
operate on one relation.
• other three operations operate on pairs of relations and are, therefore, called
binary operations.
The Select Operation(σ)
The select operation selects tuples that satisfy a given predicate. We use the
lowercase Greek letter sigma (σ) to denote selection. The predicate appears as a
subscript to σ. The argument relation is in parentheses after the σ.
For example, to select those tuples of the Instructor relation where the faculty is in
the “Physics” department, we write:
σdept_name=“Physics”(Instructor)
Relational Algebra
Select ()_ Relation Algebra
Example 2:
Example3: using And (^)
Project
The Project Operation(pi (∏)):
The project operation is a unary operation that returns its argument
relation , with certain attributes left out. Since a relation is a set, any duplicate rows
are eliminated.