SQL Commands
Creating a database:
CREATE DATABASE databasename;
To connect to a database:
\c databasename;
To delete a database:
DROP DATABASE database_name;
Creating table:
CREATE TABLE databasename.table_name (
column1 int,
column2 varchar(5)
);
Insert values into table:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
SQL Commands 1
To delete complete table:
DROP TABLE table_name [drop_behavior];
two drop behavior options
1. Cascade: All constraints that references the table are dropped
automatically along with the table itself.
2. Restrict: Table is dropped only it is not referenced in any
constraints.
To erase all the contents from the table, but the structure remains same:
TRUNCATE TABLE table_name;
Used to delete a row based on conditions:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
The table before the above statement:
SQL Commands 2
Table after using Delete command
Alter General syntax:
ALTER TABLE table_name
[add|drop|rename] column column_name;
Adding new column:
ALTER TABLE table_name
ADD column_name datatype;
Deleting a column:
SQL Commands 3
ALTER TABLE table_name
DROP COLUMN column_name;
Rename a table
ALTER TABLE table_name
RENAME TO new_table_name;
Renaming a column:
ALTER TABLE Student
RENAME COLUMN NAME TO FIRST_NAME;
Update command:
UPDATE table_name
SET marks=100
WHERE roll_number=25;
Retrieval Queries:
To display Complete-table:
SQL Commands 4
select * from table_name;
here, * => "every row"
To display a column:
select column_name from table_name;
Another example:
select * from table_name where column_name='Avinash';
SQL Commands 5
Display from ceratin conditions:
select column_name from table_name
where marks>=value1 and marks<=value2;
Emkonni examples:
SQL Commands 6
Very important example:
can also write as:
Can also be written as
SQL Commands 7
Distinct Command
SELECT DISTINCT column_name
FROM table_name;
SQL Commands 8
Order By Command:
SELECT column_name FROM table_name
ORDER BY column_name ASC|DESC;
SQL Commands 9
SQL Alias: We can give a table or a column another name by using an alias
SQL Alias Syntax for Columns:
SELECT column_name AS alias_name
FROM table_name;
SQL Commands 10
WHERE clause:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition]
Example:
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000;
Here [condition] can be any of the below:
SQL Commands 11
Wildcard Characters
underscore( _ ): Represents exactly one character
percent sign(%): Represents zero or more characters
LIKE Operator: used in a WHERE clause to search for a specified pattern in a
column.
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Example:
SQL Commands 12
IN Operator: IN operator is a shorthand for multiple OR conditions.
SQL Commands 13
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:
Example 2: both commands are same here
BETWEEN operator: BETWEEN operator selects values within a given range.
SELECT column_name(s)
FROM table_name
SQL Commands 14
WHERE column_name BETWEEN value1 AND value2;
Aggregate Functions:
SQL aggregate functions return a single value, calculated from values in a
column.
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
Examples:
SQL Commands 15
Group by statement:
SELECT aggregate_function(column_name), column_name
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Example:
SQL Commands 16
HAVING Clause:
The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
SELECT column_name FROM table_name
WHERE condition
GROUP BY column_name HAVING condition
ORDER BY column_name;
Example:
SQL Commands 17
SQL Commands 18
Summary:
Next session
TCL commands:
COMMIT: Commit command is used to permanently save any transaction into the
database.
COMMIT;
ROLLBACK: This command restores the database to the last committed state. It is
also used with the savepoint command to jump to a save point in a transaction.
ROLLBACK TO savepoint_name;
SAVEPOINT: Savepoint command is used to temporarily save a transaction so that
you can roll back to that point whenever necessary.
SQL Commands 19
SAVEPOINT savepoint_name;
More Complex SQL queries:
Example:
SQL Commands 20
SQL Commands 21
the above code can also be written as:
SQL Commands 22
ALL operator:
??
??
SQL JOIN statement:
The SQL Joins clause is used to combine records from two or more tables. where
they having common columns
A JOIN is a means for combining fields from two tables by using values common to
each
SQL Commands 23
SQL Commands 24
SQL Commands 25