SQL Queries
SQL Queries
------------
SELECT Distinct(a.salary)
From Salary A Where 2 =(
Select COUNT(distinct(Salary)) FROM salary b WHERE
a.salary <= b.salary )
Tables:
----------
Note:
-----
Round():
--------
ORDER BY:
---------
select S_FatherName from AT_Student ORDER BY S_FatherName
select S_FatherName from AT_Student ORDER BY S_FatherName DESC
AND OR:
-------
select S_Student_Name from AT_Student where S_Student_Name = 'Prakash'
AND S_FatherName = 'Kumar'
select S_Student_Name from AT_Student where S_Student_Name = 'Prakash'
OR S_FatherName = 'Kumar'
IN:
---
select S_Student_Name,S_FatherName from AT_Student where S_Student_Name
IN ('Prakash', 'BALAMURUGAN')
BETWEEN AND:
------------
select S_Student_Name from AT_Student where S_DateofJoining BETWEEN
'1/1/2005' AND '12/31/2005'
select S_Student_Name from AT_Student where S_DateofJoining NOT BETWEEN
'1/1/2005' AND '12/31/2005'
AS (Column Alias):
------------------
select S_Student_Name AS Name , S_FatherName AS FtherName from
AT_Student
AS (Table Alias):
-----------------
select S_Student_Name, S_FatherName from AT_Student AS StudentDetails
Join:
-----
select AT_Student.S_Student_Name ,
AT_Student_Registration.SR_RegistrationYear from
AT_Student, AT_Student_Registration
where AT_Student.S_Student_ID =
AT_Student_Registration.SR_Student_ID
INNER JOIN:
-----------
-- SELECT field1, field2, field3
-- FROM first_table
-- INNER JOIN second_table
-- ON first_table.keyfield = second_table.foreign_keyfield
-- The INNER JOIN returns all rows from both tables where there is a
match.
-- If there are rows in Employees that do not have matches in Orders,
those rows will not be listed.
LEFT JOIN:
----------
-- SELECT field1, field2, field3
-- FROM first_table
-- LEFT JOIN second_table
-- ON first_table.keyfield = second_table.foreign_keyfield
-- The LEFT JOIN returns all the rows from the first table (Employees),
-- even if there are no matches in the second table (Orders).
-- If there are rows in Employees that do not have matches in Orders,
those rows also will be listed.
RIGHT JOIN:
-----------
-- SELECT field1, field2, field3
-- FROM first_table
-- RIGHT JOIN second_table
-- ON first_table.keyfield = second_table.foreign_keyfield
-- The RIGHT JOIN returns all the rows from the second table (Orders),
-- even if there are no matches in the first table (Employees).
-- If there had been any rows in Orders that did not have matches in
Employees,
-- those rows also would have been listed.
UNION:
-----
-- The UNION command is used to select related information from two
tables,
-- much like the JOIN command. However,
-- when using the UNION command all selected columns need to be of the
same data type.
select S_Student_Name from AT_Student
UNION
select SR_Faculty_ADSID from AT_Student_Registration
Create a Table:
---------------
-- CREATE TABLE table_name(column_name1 data_type,column_name2
data_type,.......)
USE KUMS
INDEX:
------
-- CREATE UNIQUE INDEX index_name ON table_name (column_name)
-- CREATE INDEX index_name ON table_name (column_name)
DROP :
-----
Truncate a Table:
----------------
-- What if we only want to get rid of the data inside a table, and
not the table itself?
-- Use the TRUNCATE TABLE command (deletes only the data inside the
table):
ALTER TABLE:
------------
-- The ALTER TABLE statement is used to add or drop columns in an
existing table.
Group by:
---------
-- SELECT Company,SUM(Amount) FROM Sales GROUP BY Company
HAVING:
-------
-- HAVING was added to SQL because the WHERE keyword could not be
used against
-- aggregate functions (like SUM), and without HAVING...
-- it would be impossible to test for result conditions.
VIEW:
-----
CurrentDate Retrieve:
---------------------