Académique Documents
Professionnel Documents
Culture Documents
highest salary of Employee in SQL, you can either use SQL Join or Subquery to solve this problem. Here is SQL query using Subquery : select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
See How to find second highest salary in SQL for more ways to solve this problem. Question 2: SQL Query to find Max Salary from each department. Answer : SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
Question 3:Write SQL Query to display current date. Ans:SQL has built in function called GetDate() which returns current timestamp. SELECT GetDate();
Question 4:Write an SQL Query to check whether date passed to Query is date of given format or not. Ans: SQL has IsDate() function which is used to check passed value is date or not of specified format ,it returns 1(true) or 0(false) accordingly. SELECT ISDATE('1/08/13') AS "MM/DD/YY";
It will return 0 because passed date is not in correct format. Question 5: Write a SQL Query to print the name of distinct employee whose DOB is between 01/01/1960 to 31/12/1975. Ans: SELECT DISTINCT EmpName FROM Employees WHERE DOB 31/12/1975;
Question 6:Write an SQL Query find number of employees according to gender whose DOB is between 01/01/1960 to 31/12/1975.
Answer : SELECT COUNT(*), sex from Employees WHERE 01/01/1960 ' AND 31/12/1975 GROUP BY sex;
DOB BETWEEN
Question 7:Write an SQL Query to find employee whose Salary is equal or greater than 10000. Answer : SELECT EmpName FROM Employees WHERE Salary>=10000;
Question 8:Write an SQL Query to find name of employee whose name Start with M Ans: SELECT * FROM Employees WHERE EmpName like 'M%'; Question 9: find all Employee records containing the word "Joe", regardless of whether it was stored as JOE, Joe, or joe. Answer : SELECT * from Employees upper('joe%'); WHERE upper(EmpName) like
Question 10: Write a SQL Query to find year from date. Answer : SELECT YEAR(GETDATE()) as "Year";
Difference between Primary Key & Unique Key Primary Key Unique Key Primary Key can't accept null Unique key can accept only one null value. values. By default, Primary key is clustered By default, index and data in the database Unique key is a table is physically organized in the unique nonsequence of clustered index. clustered index. We can have only one Primary key We can have more than one unique key in a table. in a table. Primary key can be made foreign In SQL Server, Unique key can be made foreign key into key into another table. another table.
6. )
Primary Key Primary key uniquely identify a record in the table. Primary Key can't accept null Foreign key can accept multiple null value. values. By default, Primary key is Foreign key do not automatically create an index, clustered or
Difference between Primary Key & Foreign Key Foreign Key Foreign key is a field in the table that is primary key in another table.
clustered index and data in the database table is physically organized in the sequence of clustered index. We can have only one Primary key in a table.
Note
As @Marc Jellinek suggested, I would like to add the below points about foreign key : 4.Foreign keys do not automatically create an index, clustered or nonclustered. You must manually create an index on foreign keys. 5.There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table. What's the advantage? If you are selecting the parent plus all child records, you want the child records next to each other. This is easy to accomplish using a clustered index. 6.Having a null foreign key is usually a bad idea. In the example below, the record in [dbo].[child] is what would be referred to as an "orphan record". Think long and hard before doing this. IF EXISTS (SELECT * FROM [sys].[schemas] [sch] INNER JOIN [sys].[tables] [tbl] ON [sch].[schema_id] = [tbl].[schema_id] WHERE [sch].[name] = 'dbo' AND [tbl].[name] = 'child') DROP TABLE [dbo].[child] IF EXISTS (SELECT * FROM [sys].[schemas] [sch] INNER JOIN [sys].[tables] [tbl] ON [sch].[schema_id] = [tbl].[schema_id]
WHERE [sch].[name] = 'dbo' AND [tbl].[name] = 'parent') DROP TABLE [dbo].[parent] CREATE TABLE [dbo].[parent] ( [id] [int] IDENTITY NOT NULL, [name] [varchar](250) NOT NULL, CONSTRAINT [PK_dbo__parent] PRIMARY KEY NONCLUSTERED ([id]) ) CREATE TABLE [dbo].[child] ( [id] [int] IDENTITY NOT NULL, [parent_id] [int] NULL, [name] [varchar](250) NOT NULL, CONSTRAINT [PK_dbo__child] PRIMARY KEY NONCLUSTERED ([id]), CONSTRAINT [FK_dbo__child__dbo__parent] FOREIGN KEY ([parent_id]) REFERENCES [dbo].[parent]([id]) ) --Insert data INSERT INTO [dbo].[parent] ([name]) VALUES ('parent1') INSERT INTO [dbo].[child] ([parent_id], [name])VALUES(1, 'child 1') INSERT INTO [dbo].[child] ([parent_id], [name])VALUES(NULL, 'child 2') --Select data SELECT * FROM [dbo].[child]
SQL join clause is used to to retrieve data from two or more database tables. In previous article, I have explained the Different Types of SQL Joins. In this article, I would explain the difference among inner join, equi join and natural join.
Inner Join
This is the most used join in the SQL. this join returns only those records/rows that match/exists in both the database tables.
tblDept.Name HR IT IT ADMIN
tblDept.Dep 1 2 2 3
In the join condition, you can also use other operators like <,>,<>.
Equi Join
Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for join using equality operator then that join query comes under Equi join.
tblDept.Dep 1 2 2 3
Note
Inner join can have equality (=) and other operators (like <,>,<>) in the join condition. Equi join only have equality (=) operator in the join condition. -Equi join can be an Inner join, Left Outer join, Right Outer join The USING clause is not supported by SQL Server and Sybase. This clause is supported by Oracle and MySQL.
Natural Join
Natural join is a type of equi join which occurs implicitly by comparing all the same names columns in both tables. The join result have only one column for each pair of equally named columns.
Note
In Natural join, you can't see what columns from both the tables will be used in the join. In Natural join, you might not get the desired result what you are expecting. Natural join clause is not supported by SQL Server, it is supported by Oracle and MySQL Normalization or data normalization is a process to organize the data into tabular format (database tables). A good database design includes the normalization, without normalization a database system may slow, inefficient and might not produce the expected result. Normalization reduces the data redundancy and inconsistent data dependency.
Normal Forms
We organize the data into database tables by using normal forms rules or conditions. Normal forms help us to make a good database design. Generally we organize the data up to third normal form. We rarely use the fourth and fifth normal form. To understand normal forms consider the folowing unnormalized database table. Now we will normalize the data of below table using normal forms.
29.
30.
31.
32.
33.
34.
35.
36.
is
as
follows:
37.
38. 39.