Plsdontdeleteveryprecious SQL
Plsdontdeleteveryprecious SQL
DESC STUDENT26;
//gives description of Table
--------------------------
//to order the column and display it in ascending or descending (for descending we
use desc keyword)
select * from <Table Name> ORDER BY <Column name> <use desc for descending sort>
for eg: select * from S1 ORDER BY Marks desc
// to find dept
select disctint dept from S1;
// to delete
delete from S1;
sql>drop table S1;
sql>trunquate table S1;
sql>delete from S1 where roll = "4";
-----------------------------------
----
//Pattern matching
example: SELECT * from STUDENT26 where Name LIKE '%A'; //this will find name ending
with A.
//if there is a special symbol say @, how will we match the pattern?
----not in syll----
select * from STUDENT26 where REGEXP_LIKE (Name <i.e. the column name>,<insert
special char for which we need the pattern, in '[]'>, <insert 'i' or 'I' if you
want lowercase sensi or uppercase sensi of the other characters>)
//Logical Operators
//we have AND OR NOT BETWEEN
//for not
SELECT * from STUDENT26 where NOT (Department = 'CSE');
//for between
SELECT * from STUDENT26 where marks BETWEEN 17 AND 200;
//Aggregate function
//we have MAX MIN AVG SUM COUNT
//count means we count the number of 'NON NULL ROWS' unless we specify *
-----------------------------------------------------------
//we will do "group by" and "having" (having has same function as where)
group by --> will divide according to the group
eg:
select dept, COUNT(name) from S1 group by (dept) having COUNT(name) > 1;
to union/intersect/minus:
select NAME from T1 UNION/UNION ALL/INTERSECT/MINUS select NAME from T2;
//SUB QUERY
//nested query has inner brackets and outer brackets. inner executes first and the
outer.
for eg:
select name from S1 where marks = <can write = or IN> (select MAX (Marks) from S1);
create table T1 (Name varchar2(20), Roll Integer primary key, Dept varchar(20) NOT
NULL);
//Foriegn Key links tables and normalizes the values and removes redundancies.
create table T2 (Roll Integer REFERENCES T1, Name varchar2(20),Dept varchar(20) NOT
NULL);
-----------------------------------------
---------------
syntax:
Select S1.Name, S1.Roll, S2.d_id from S1 <Insert join name i.e. INNER JOIN, LEFT
OUTER JOIN, RIGHT OUTER JOIN etc> S2 <show primary key relation with ON> ON S1.Roll
= S2.Roll <add where clause if needed>
PlSql:
//Problem : Give account number + Check if balance is less than 5k then -100 as
fine.
\\Create table Acct-mstr with attri. Acct_no varchar(20) and cur_bal number (11,2)
------------------------
#Create user.
create user <username> identified by <password>;
#Revoke
revoke all on <table name> from <username>;
#
select * from <mother user>.<tablename> (this is to access the table OF mother
user. basically jekhane databse ache oita acess korte chae).