sql-notes-queries-1 (1)
sql-notes-queries-1 (1)
LIKE: This operator is used with the WHERE clause to search for a specified pattern in a column
containing the string.
‘A%’ – string starts with A
‘&A’ – ends with A
‘%A%’ – A will be in between the string
‘_A%’ – Here the 2nd letter will be A
‘%A_’ – The 2nd from the last letter will be A
LIMIT: This is used to specify the number of records we want after executing the query. If we want
the top 5 students of a class, then after sorting the results, we can use this LIMIT by specifying 5. So
that it will only fetch the top 5 records.
47)We can use having like where clause with aggregate fns. ( We can not use aggregate fns in where
clause) But the conditional fields should have been referred using select clause.
a) select age from emp having max(age);
b) select age from emp having max(age) and age >= 25 and age <=30;
c) select age from emp having max(age) and name like 'r%'; # Error
d) select age from emp having max(age) and id >= 101 and id <= 110; # Error
e) select age, id from emp having max(age) and id >= 101 and id <= 110;
f) select age, id, name from emp having max(age) and id >= 101 and id <= 110 and name like 'R%';
g) select age, id, name from emp having max(age) and id >= 101 and id <= 110 and name like 'K%';
h) Select age, id, name, dob from emp having max(age) and id >= 101 and id <= 110 and name like
'R%';
49) SELECT ID , CONCAT(name, ' WHOSE DOB ON ', dob) AS 'INFORMATION' FROM EMP;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Creating a new table from an already existing table using CREATE TABLE.
The new table gets the same column definitions. All columns or specific columns can be selected.If
you create a new table using an existing table, the new table will be filled with the existing values
from the old table.
54) CREATE TABLE TEST AS SELECT ID, NAME, CITY FROM EMPLOYEE ;
This SELECT command can also be used with the INSERT command which is used for adding
records to the existing table.
The INSERT INTO SELECT statement copies data from one table and inserts it into
another table.
The INSERT INTO SELECT statement requires that the data types in source and target
tables matches.
Table 2 should have been already created with the respective attributes
CREATE TABLE Employee (ID INT, Name VARCHAR(255),City CHAR(200), DOB DATE);
SUBSTRING: Used to pick a specific character from a string by specifying the position.
1) SELECT SUBSTRING(name,1,5) FROM emp;
(it will fetch character from 1st to 5th position of attribute nameg)
2) INSTR: This returns a position of a string in another string.
SELECT INSTR(‘independence’, ‘pen’);
(it will find the position of ‘pen’ in the word ‘independence’)
SELECT INSTR(NAME,'a') FROM EMP; # Not case sensitive
3)LCASE(string): Returns the lower case of the string. same as Lower(string)
Group by - having
61) SELECT SUM(SALARY) FROM EMP GROUP BY SEX;
62) SELECT DEPT, COUNT(*) FROM EMP GROUP BY DEPT HAVING SUM(SALARY) >
20000;
63) SELECT DEPT, COUNT(*) FROM EMP WHERE SEX = 'F' GROUP BY DEPT HAVING
SUM(SALARY) > 20000;
Join
64) SELECT * FROM EMP1, EMP2;
65) SELECT EMP1.ID, EMP1.NAME, EMP1.AGE, EMP1.
66) SELECT EMP1.ID, EMP1.NAME, EMP1.AGE, EMP1.SALARY, EMP2.ID, EMP2.COMM
FROM EMP1, EMP2;
67) SELECT * FROM EMP1, EMP2 WHERE EMP1.ID = EMP2.ID;
68) SELECT EMP1.ID, NAME, AGE, SALARY, EMP2.ID, COMM FROM EMP1, EMP2;
69) SELECT EMP1.*, EMP2.*, COMM FROM EMP1, EMP2;
71) SELECT EMP1.ID, NAME, COMM FROM EMP1 , EMP2 WHERE EMP1.ID = EMP2.ID;
72) SELECT EMP1.ID, NAME, COMM FROM EMP1 , EMP2 WHERE EMP1.ID = EMP2.ID
AND AGE >= 25;
73) SELECT A.*, B.* FROM EMP1 A, EMP2 B WHERE A.ID = B.ID;
74) SELECT EMP1.ID, NAME, COMM FROM EMP1 , EMP2 WHERE EMP1.ID < > EMP2.ID;
75) SELECT EMP1.ID, NAME, COMM FROM EMP1 , EMP2 WHERE EMP1.ID != EMP2.ID;