0% found this document useful (0 votes)
4 views5 pages

Notes: Basic Statements

Uploaded by

titeer123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Notes: Basic Statements

Uploaded by

titeer123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL

Notes

● Basic statements

SHOW DATABASES;

CREATE DATABASE <name>;

USE <database name>;

DROP DATABASE;

SHOW TABLES;

DESC <tablename>;

DROP TABLE <tablename>;

● Adding primary key

ALTER TABLE <tname> ADD PRIMARY KEY (<attributename>);

ALTER TABLE <tname> ADD PRIMARY KEY (<att1name, att2name>);

The above one is for composite key

ALTER TABLE <tname> ADD FOREIGN KEY (<attribute name>) REFERENCES


<referencedtname> (<attribute name>);

● Altering an existing attribute

To add new unique constraint:

ALTER TABLE <tname> ADD UNIQUE (<attname>);

To add attribute in table:

ALTER TABLE <tname> ADD <attname> DATATYPE;

To alter datatype of the attribute:

ALTER TABLE <tname> MODIFY <attname> NEWDATATYPE;

To add not null constraint

ALTER TABLE <tname> MODIFY <attname> DATATYPE NOT NULL;


To add DEFAULT constraint

ALTER TABLE <tname> MODIFY <attname> DATATYPE DEFAULT ‘<default


vale>’;

To drop attribute or primary key

ALTER TABLE <tname> DROP <attname>;

ALTER TABLE <tname> DROP PRIMARY KEY;

● Creating table

CREATE TABLE <tname> (att1 datatype PRIMARY KEY, att2 datatype, …);

● Custom condition : Check

Dt DATE CHECK (dt>=‘ 2020/01/01’);

● Inserting Values

INSERT INTO <tname> VALUES(v1,v2…);

INSERT INTO <tname> (column1, colum2,...) VALUES(v1,v2…);

INSERT INTO <tname> VALUES(v1,v2,NULL…);

If no value is provided

● Select statement

For all data

SELECT * FROM <tname>;

SELECT att1, att2 FROM <tname>;

SELECT att1, att2 FROM <tname> WHERE <condition>;

SELECT DISTINCT att2 FROM <tname>;

SELECT * FROM <tname> WHERE NOT Ename=‘ ’;

SELECT att1 AS <newattname> FROM <tname>;

SELECT att1 AS ‘<newattname1>’, att2 AS ‘<newattname2>’ FROM <tname>;

Selecting columns null values appear as NULL

SELECT name,birth, IFFNULL(deathdate, ‘Alive’) AS ‘Died on’ FROM pet;


● AND, OR, BETWEEN

SELECT <attname> FROM <tname> WHERE <condition> OR <condition>;

SELECT <attname> FROM <tname> WHERE <condition> AND <condition>;

SELECT <attname> FROM <tname> WHERE salary BETWEEN 2000 AND 5000;

● Membership operator IN

(The IN operator compares a value with a set of values and returns true if the
value belongs to that set.)

SELECT * FROM <tname> WHERE <attname> IN (‘value1’, ‘value2’);

SELECT * FROM <tname> WHERE <attname> NOT IN (‘value1’, ‘value2’);

● Order by clause

SELECT * FROM <tname> ORDER BY <attname>;

SELECT * FROM <tname> ORDER BY <attname> DESC;

● Null/ not null

SELECT * FROM <tname> WHERE <attname> IS NULL;

SELECT * FROM <tname> WHERE <attname> IS NOT NULL;

● LIKE OPERATOR

% - zero or multiple character

_- exactly one character

SELECT * FROM <tname> WHERE <attname> LIKE

‘%k’ - last letter is k

‘k%’ - first letter is k

‘_P’ - single letter in front and ending with P

‘_POL%’ - single letter in front and ending with as many letters but has to
contain POL inside it

‘%s%’ - has to contain s

● Update statement

UPDATE <tname> SET att1=Value 1, att2 = Value2 WHERE <condition>;

UPDATE STUDENT SET Address = 'Azad Avenue', Roll = 9 WHERE STUID=2226;


● Deleting one or more records

DELETE FROM <tname> WHERE <condition>;

● Aggregate functions

SELECT AVG(salary) “Average” FROM employee;

SELECT COUNT(*) “Total” FROM employee;

The above includes the null values, other count statements don’t

SELECT COUNT(job) “Total jobs” FROM employee;

SELECT COUNT(<attname>) FROM <tname> WHERE <condition>;

SELECT COUNT(DISTINCT job) “Distinct jobs” FROM employee;

SELECT COUNT (ALL city) FROM members;

SELECT MAX(salary) “Maximum salary” FROM employee;

SELECT MIN(salary) “Minimum salary” FROM employee;

SELECT SUM(salary) “Total salary” FROM employee;

● GROUP BY CLAUSE

It groups the rows together that contain the same values in a specified
column.

SELECT CustomerID, COUNT(*) FROM customers GROUP BY CustomerID;

SELECT deptno, COUNT(*),SUM(Salary) FROM employee GROUP BY deptno;

This will display the no. of employees in each department under deptno and
find the sum of salaries of employees belonging to the same department.

SELECT CustomerID, COUNT(CustomerID) FROM customers GROUP BY


CustomerID ORDER BY CustomerID;

The ORDER BY clause decides the order in which the fields in the column are
displayed while the GROUP BY clause decides the column in which the rows
need to be grouped

USING THE HAVING CLAUSE:

SELECT CustomerID, COUNT(*) FROM customers GROUP BY CustomerID


HAVING Count(*)>1;

SELECT PaymentMode, Count(PaymentMode) FROM SALE GROUP BY


Paymentmode HAVING COUNT(*)>1 ORDER BY Paymentmode;
● ting table

You might also like