You are on page 1of 11

SQL QUERIES

1.NULL
SELECT * FROM Employees WHERE ManagerId IS NULL ;
2. SELECT * FROM Employees WHERE ManagerId IS NOT NULL ;
3. When creating tables it is possible to declare a column as nullable or non-nullable.
4. CREATE TABLE MyTable ( MyCol1 INT NOT NULL, -- non-nullable MyCol2 INT
NULL -- nullable ) ;
5. Attempting to assign NULL to a non-nullable column will result in an error. INSERT
INTO MyTable (MyCol1, MyCol2) VALUES (1, NULL) ;
6.
Setting a field to NULL works exactly like with any other value:
UPDATE Employees SET ManagerId = NULL WHERE Id = 4;
7. CREATE TABLE Departments ( Id INT NOT NULL AUTO_INCREMENT, Name
VARCHAR(25) NOT NULL, PRIMARY KEY(Id) );
INSERT INTO Departments ([Id], [Name]) VALUES (1, 'HR'), (2, 'Sales'), (3, 'Tech');
8.
CREATE TABLE Employees (
Id INT NOT NULL AUTO_INCREMENT,
FName VARCHAR(35) NOT NULL,
LName VARCHAR(35) NOT NULL,
PhoneNumber VARCHAR(11),
ManagerId INT,
DepartmentId INT NOT NULL,
Salary INT NOT NULL,
HireDate DATETIME NOT NULL,
PRIMARY KEY(Id),
FOREIGN KEY (ManagerId) REFERENCES Employees(Id),
FOREIGN KEY (DepartmentId) REFERENCES Departments(Id)
);
INSERT INTO Employees ([Id], [FName], [LName], [PhoneNumber], [ManagerId],
[DepartmentId], [Salary], [HireDate])
VALUES (1, 'James', 'Smith', 1234567890, NULL, 1, 1000, '01-01-2002'), (2, 'John',
'Johnson', 2468101214, '1', 1, 400, '23-03-2005'), (3, 'Michael', 'Williams', 1357911131, '1', 2,
600, '12-05-2009'), (4, 'Johnathon', 'Smith', 1212121212, '2', 1, 500, '24-07-2016');
View all books and their authors
SELECT ba.AuthorId, a.Name AuthorName, ba.BookId, b.Title BookTitle FROM
BooksAuthors ba INNER JOIN Authors a ON a.id = ba.authorid INNER JOIN Books b ON
b.id = ba.bookid.
9. To select all values from a specific table, the wildcard character can be applied to the table
with dot notation.

Consider the following query:


SELECT Employees.*, Departments.Name
FROM Employees JOIN Departments ON Departments.Id = Employees.DeptI
This will return a data set with all fields on the Employee table, followed by just the Name field in
the Departments table.

10. Aliases can be created in all versions of SQL using double quotes (").

SELECT Fname AS “First Name”,Mname AS “Middle Name”,Lname AS Last Name” From employees;

10. If multiple tables are joined together, you can select columns from specific tables by specifying
the table name before the column name: [table_name].[column_name].

SELECT Customers.PhoneNumber, Customers.Email, Customers.PreferredContact, Orders.Id AS


OrderId FROM Customers LEFT JOIN Orders ON Orders.CustomerId = Customers.Id

Select with condition of multiple values from column.

SELECT * FROM Cars WHERE status IN ( 'Waiting', 'Working' )


This is semantically equivalent to
SELECT * FROM Cars WHERE ( status = 'Waiting' OR status = 'Working'.
A HAVING clause filters the results of a GROUP BY expression. Note: The following examples are
using the Library example database. Examples: Return all authors that wrote more than one book
(live example). SELECT a.Id, a.Name, COUNT(*) BooksWritten FROM BooksAuthors ba INNER JOIN
Authors a ON a.id = ba.authorid GROUP BY a.Id, a.Name HAVING COUNT(*) > 1 -- equals to HAVING
BooksWritten > 1 ;
Normalization is the process of minimizing redundancy and dependency by
organizing fields and table of a database. The main aim of Normalization is to
add, delete or modify field that can be made in a single table.
DeNormalization is a technique used to access the data from higher to lower
normal forms of database. It is also process of introducing redundancy into a
table by incorporating data from the related tables.

 First Normal Form (1NF):.

This should remove all the duplicate columns from the table. Creation of tables
for the related data and identification of unique columns.

 Second Normal Form (2NF):.

Meeting all requirements of the first normal form. Placing the subsets of data in
separate tables and Creation of relationships between the tables using primary
keys.


Third Normal Form (3NF):.

This should meet all requirements of 2NF. Removing the columns which are not
dependent on primary key constraints.

 Fourth Normal Form (4NF):.

If no database table instance contains two or more, independent and


multivalued data describing the relevant entity, then it is in 4th Normal Form.

 Fifth Normal Form (5NF):.


A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed
into any number of smaller tables without loss of data.

 Sixth Normal Form (6NF):.

6th Normal Form is not standardized, yet however, it is being discussed by


database experts for some time. Hopefully, we would have a clear &
standardized definition for 6th Normal Form in the near future…

What is a View?
A view is a virtual table which consists of a subset of data contained in a table.
Views are not virtually present, and it takes less space to store. View can have
data of one or more tables combined, and it is depending on the relationship.

What is an Index?
An index is performance tuning method of allowing faster retrieval of records
from the table. An index creates an entry for each value and it will be faster to
retrieve data.

What are all the different types of indexes?


There are three types of indexes -.

1.Unique Index.

This indexing does not allow the field to have duplicate values if the column is
unique indexed. Unique index can be applied automatically when primary key is
defined.

2. Clustered Index.

This type of index reorders the physical order of the table and search based on
the key values. Each table can have only one clustered index.

3.NonClustered Index.

Non-Clustered Index does not alter the physical order of the table and maintains
logical order of data. Each table can have 999 non-clustered indexes.

Scheme is used to define overall design of the database


Database Application

1. Telecom
2. Industry
3. Bank
4. Sales
5. Education sector

You might also like