Basic SQL
Basic SQL
Basic SQL
101 SCIENCE
102 ENGLISH
103
----------CONSTRAINTS---------
SQL CONSTRAINTS ARE USED TO SPECIFY RULES FOR DATA IN TABLE.
-----------WHERE CLAUSE------------
TO DEFINE SOME CONDITIONS
OPERATORS
AND (to check for both conditons to be true)
SELECT * FROM student WHERE marks > 80 AND city = "Mumbai";
---------------------LIMIT CLAUSE----------------
Sets an upper limit on number of (tuples) rows to be returned
----------------------ORDER BY CLAUSE-----------
to sort in ascending (ASC) or decending order (DESC);
SELECT * FROM student ORDER BY city ASC;
-----------------AGGREGATE FUNCTIONS-----------
aggregate function perform a calculation on a set of values, and return a single value.
COUNT()
MAX()
MIN()
SUM()
------------GROUP BY CLAUSE----------------------
group rows that have the same values into summary rows.
it collcts data from multiple records and group the result by one or more column.
example:-
Count number of students in each city
SELECT city,count(rollno)
FROM student
GROUP BY city;
SELECT city,name,count(rollno)
FROM student
GROUP BY city,name;
-----------------HAVING CLAUSE------------------------
similar to where i.e. applies some condition on rows.
used when we want to apply any condition after grouping.
Count number of students in each city where max marks cross 90.
SELECT count(name),city
FROM student GROUP BY city
HAVING max(marks)>90;
----------------------GENERAL ORDER--------------------
SELECT columns(s)
FROM table_name
WHERE conditon
GROUP BY columns(s)
HAVING condition
ORDER BY column(s) ASC;
example
SELECT city
FROM student
WHERE grade >= "A"
GROUP BY city
HAVING MAX(marks) >=93
ORDER BY city DESC;
UPDATE table_namw
SET col1 = val1, col2 = val2
WHERE condition;
EXAMPLE:-
UPDATE student
SET grade = "O"
WHERE grade = "A";
UPDATE student
SET marks = marks + 1;
// NO WHERE CONDITIONS IS REQUIRED
-----------------------ALTER-----------------------------------
ALTER (to change the schema)
• ADD column
ALTER TABLE table_name
ADD COLUMN column_name datatype constraint;
• DROP column
ALTER TABLE table_name
DROP COLUMN column_name;
• RENAME table
ALTER TABLE table_name
RENAME TO new_table_name;
• CHANGE Column(rename)
ALTER TABE table_name
CHANGE COLUMN old_name new_name new_datatype new _constraint;
EXAMPLE:-
-----------------------TRUNCATE---------------------------------
TRUNCATE(to delete table's data)