SELECT
, etc.SELECT registration_no FROM students;
SELECT
, INSERT
, UPDATE
, DELETE
, ALTER
, DROP
etc. CREATE DATABASE database_name;
-- You can use this command to list all databases
SHOW DATABASES;
CREATE TABLE TABLE_NAME(COLUMN_NAME DATATYPES[,....]);
-- You can use this command to list all tables in a database
SHOW TABLES;
-- To see structure of a table you can use following command
DESCRIBE table_name;
-- OR
DESC table_name;
CREATE TABLE EMPLOYEE(Name VARCHAR(20), Email VARCHAR(100), DOB DATE);
DROP TABLE TABLE_NAME;
DROP TABLE EMPLOYEE;
ALTER TABLE table_name ADD column_name (COLUMN DEFINITION);
ALTER TABLE table_name MODIFY (COLUMN DEFINITION....);
ALTER TABLE STU_DETAILS ADD (ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
TRUNCATE TABLE table_name;
TRUNCATE TABLE EMPLOYEE;
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, ...., valueN);
INSERT INTO students (Name, Subject) VALUES ("Alice", "DBMS");
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION];
UPDATE students SET Name = 'Bob' WHERE Student_Id = '3';
DELETE FROM table_name [WHERE condition];
DELETE FROM students WHERE Name="Bob";
WHERE
clause.
SELECT expressions FROM TABLES WHERE conditions;
SELECT emp_name FROM employee WHERE age > 20;
-- Primary Key using single column
CREATE TABLE students
(
S_Id int NOT NULL,
LastName varchar (255) NOT NULL,
FirstName varchar (255),
Address varchar (255),
City varchar (255),
PRIMARY KEY (S_Id)
)
-- Primary Key using multiple columns
CREATE TABLE students
(
S_Id int NOT NULL,
LastName varchar (255) NOT NULL,
FirstName varchar (255),
Address varchar (255),
City varchar (255),
CONSTRAINT pk_StudentID PRIMARY KEY (S_Id, LastName)
)
CREATE TABLE orders
(
O_Id int NOT NULL,
Order_No int NOT NULL,
S_Id int,
PRIMAY KEY (O_Id),
FOREIGN KEY (S_Id) REFERENCES Persons (S_Id)
)
WHERE
clause in SQL is a data manipulation language statement.=
, >
, <
, <=
, >=
, <>
.<>
means not equal to. SELECT s_name FROM students WHERE s_age >= 18;
AND
and OR
clauses can be used to group multiple conditions. SELECT s_name, s_age FROM students WHERE (s_age >= 18 AND cpi > 7.0);
IN
operator allows you to specify multiple values in a WHERE
clause.OR
conditions. -- Without Using IN
SELECT s_name FROM students WHERE (s_age=18 OR s_age=19 OR s_age=20);
-- Using IN
SELECT s_name FROM students WHERE s_age IN (18,19,20);
SELECT s_name FROM students WHERE s_age NOT IN (18,19,20);
SELECT s_name FROM students WHERE s_age BETWEEN 18 AND 20;
-- Using AND, NOT, IN and BETWEEN
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
LIKE
clause is used to compare a value to similar values using wildcard operators. -- Find all students whose name start with letter A.
SELECT * FROM students WHERE s_name LIKE "A%";
-- Find all employess whose salary end with digit 2.
SELECT * FROM employees WHERE emp_salary LIKE "%2";
-- Find all employees whose salary has digit 0 at second and third position from starting.
SELECT * FROM employees WHERE emp_salary LIKE "_00%";
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1, column2... ASC|DESC;
SELECT * FROM students ORDER BY s_age DESC;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name
WHERE condition;
GROUP BY
clause groups rows that have the same values into summary rows, like “find the number of customers in each country”.GROUP BY
clause is often used with aggregate functions (COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
) to group the result-set by one or more columns.SELECT
statement, the GROUP BY
clause follows the WHERE
clause and precedes the ORDER BY
clause. SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
HAVING
clause was added to SQL because the WHERE
clause cannot be used with aggregate functions. SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
WHERE Continent="Asia"
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
=
, <
, or <>
.IS NULL
and IS NOT NULL
operators instead. -- IS NULL
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
-- IS NOT NULL
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
-- Column Name Alias
SELECT column_name AS alias_name
FROM table_name;
-- Table Name Alias
SELECT column_name(s)
FROM table_name AS alias_name;
-- OR
SELECT column_name(s)
FROM table_name alias_name;
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;