Cid Bid Maname Mid Sid Bid, Cid, Sid: Print The Sid of All Salespeople Who Have Sold Both A Ford and A Toyota in 1997
Cid Bid Maname Mid Sid Bid, Cid, Sid: Print The Sid of All Salespeople Who Have Sold Both A Ford and A Toyota in 1997
Print the sid of all salespeople who have sold both a Ford and a Toyota in 1997.
only those salesamn who sold both ford & toyota or one of them
in 1997 and toyota & ford are model name or maname
any how i am replying both questions......
Ans Q#1
==================
selct distinct t.sid,s.sname
from transactions t,salespeople s,cars c,models m,MANUFACTURERS ma
where t.sid = s.sid and
t.cid = c.cid and
c.mid = m.mid
m.maname = ma.maname
and t.year = 1997
and mn.mname in ('Toyota','Ford')
Result------------------
SID SNAME CID CYEAR MID MODEL MANAME
-----------------------------------------------------------------------
Ans of Q#2
------------------------------
select t.sid,s.sname,to_char(sum(t.amount),'fm$999999.00') tot_amount
from transactions t, salespeople s
where t.sid = s.sid and
year = 1997
group by sid,year
having sum(t.amount)
in
(
select max(t_amount) max_amount from
(
select sum(amount) t_amount from transactions
where year = 1997
group by sid,year
)
)
result query 2
========================
2. Print the sname and total sales amount of the salesperson who had the highest total sales (in
dollars) for 1997.
Comparision
Description
Operators
column value is similar to specified
LIKE
character(s).
column value is equal to any one of
IN
a specified set of values.
column value is between two
BETWEEN...AND values, including the end values
specified in the range.
IS NULL column value does not exist.
For example: To select all the students whose name begins with 'S'
first_name last_name
------------- -------------
Stephen Fleming
Shekar Gowda
The above select statement searches for all the rows where the first letter of the column
first_name is 'S' and rest of the letters in the name can be any character.
There is another wildcard character you can use with LIKE operator. It is the underscore
character, ' _ ' . In a search string, the underscore signifies a single character.
For example: to display all the names with 'a' second character,
SELECT first_name, last_name
FROM student_details
WHERE first_name LIKE '_a%';
first_name last_name
------------- -------------
Rahul Sharma
NOTE:Each underscore act as a placeholder for only one character. So you can use more than
one underscore. Eg: ' __i% '-this has two underscores towards the left, 'S__j%' - this has two
underscores between character 'S' and 'i'.
For Example: to find the names of the students between age 10 to 15 years, the query would be
like,
SQL IN Operator:
The IN operator is used when you want to compare a column with more than one value. It is
similar to an OR condition.
For example: If you want to find the names of students who are studying either Maths or
Science, the query would be like,
For Example: If you want to find the names of students who do not participate in any games,
the query would be as given below
There would be no output as we have every student participate in a game in the table
student_details, else the names of the students who do not participate in any games would be
displayed.
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
For Example: If you want to sort the employee table by salary of the employee, the sql query
would be.
name salary
---------- ----------
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000
The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.
If you want to sort the employee table by the name and salary, the query would be like,
name salary
------------- -------------
Soumya 20000
Ramesh 25000
Priya 30000
Harsha 35000
Hrithik 35000
NOTE:The columns specified in ORDER BY clause should be one of the columns selected in
the SELECT column list.
You can represent the columns in the ORDER BY clause by specifying the position of a column
in the SELECT list, instead of writing the column name.
By default, the ORDER BY Clause sorts data in ascending order. If you want to sort the data in
descending order, you must explicitly specify it as shown below.
The above query sorts only the column 'salary' in descending order and the column 'name' by
ascending order.
If you want to select both name and salary in descending order, the query would be as given
below.
SELECT name, salary
FROM employee
ORDER BY name DESC, salary DESC;
For example: If you want to display employee name, current salary, and a 20% increase in the
salary for only those employees for whom the percentage increase in salary is greater than 30000
and in descending order of the increased price, the SELECT statement can be written as shown
below
SQL COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified, then the
query returns the total number of rows in the table.
For Example: If you want the number of employees in a particular department, the query would
be:
If you want the total number of employees in all the department, the query would take the form:
For Example: If you want to select all distinct department names from employee table, the
query would be:
To get the count of employees with unique name, the query would be:
SQL MAX(): This function is used to get the maximum value from a column.
To get the maximum salary drawn by an employee, the query would be:
SQL MIN(): This function is used to get the minimum value from a column.
SQL AVG(): This function is used to get the average value of a numeric column.
SQL SUM(): This function is used to get the sum of a numeric column
For Example: If you want to know the total amount of salary spent on each department, the
query would be:
dept salary
---------------
--------------
-
Electrical 25000
Electronics 55000
Aeronautics 35000
InfoTech 30000
NOTE: The group by clause should contain all the columns in the select list expect those used
along with the group functions.
For Example: If you want to select the department that has total salary paid for its employees
more than 25000, the sql query would be like;
dept salary
------------- -------------
Electronics 55000
Aeronautics 35000
InfoTech 30000
When WHERE, GROUP BY and HAVING clauses are used together in a SELECT statement,
the WHERE clause is processed first, then the rows that are returned after the WHERE clause is
executed are grouped based on the GROUP BY clause. Finally, any conditions on the group
functions in the HAVING clause are applied to the grouped rows before the final output is
displayed.
For Example: To drop the column "location" from the employee table, the query would be like
For Example: To modify the column salary in the employee table, the query would be like
If you change the object's name any reference to the old name will be affected. You have to
manually change the old name to the new name in every reference.
For Example: To change the name of the table employee to my_employee, the query would be
like
L Integrity Constraints
Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.
column_name1, column_name2 are the names of the columns which define the primary
Key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional.
For Example: To create an employee table with Primary Key constraint, the query would be
like.
or
For Example:
2) If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary
key 'id' within the same table, the query would be like,
For Example: To create a employee table with Null value, the query would be like
For Example: To create an employee table with Unique key, the query would be like,
or
For Example: In the employee table to select the gender of a person, the query would be like
If a sql join condition is omitted or if it is invalid the join operation will result in a Cartesian
product. The Cartesian product returns a number of rows equal to the product of all rows in all
the tables being joined. For example, if the first table has 20 rows and the second table has 10
rows, the result will be 20 * 10, or 200 rows. This query takes a long time to execute.
Lets use the below two tables to explain the sql join conditions.
SQL Joins can be classified into Equi join and Non Equi join.
It is a simple sql join condition which uses the equal sign as the comparison operator. Two types
of equi joins are SQL Outer join and SQL Inner join.
For example: You can get the information about a customer who purchased a product and the
quantity of product.
It is a sql join condition which makes use of some comparison operator other than the equal sign
like >, <, >=, <=
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on
a relationship between certain columns in these tables.
A primary key is a column (or a combination of columns) with a unique value for each row.
Each primary key value must be unique within the table. The purpose is to bind data together,
across tables, without repeating all of the data in every table.
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two
rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same
name.
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id" column
refers to the persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right
table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
FULL JOIN: Return rows when there is a match in one of the tables
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Now we want to list all the persons and their orders - if any, from the tables above.
Svendson Tove
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no
matches in the right table (Orders).
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Now we want to list all the orders with containing persons - if any, from the tables above.
34764
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no
matches in the left table (Persons).
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Now we want to list all the persons and their orders, and all the orders with their persons.
Svendson Tove
34764
The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows from
the right table (Orders). If there are rows in "Persons" that do not have matches in "Orders", or if
there are rows in "Orders" that do not have matches in "Persons", those rows will be listed as
well.
Notice that each SELECT statement within the UNION must have the same number of columns.
The columns must also have similar data types. Also, the columns in each SELECT statement
must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate values,
use UNION ALL.
PS: The column names in the result-set of a UNION are always equal to the column names in the
first SELECT statement in the UNION.
"Employees_Norway":
E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
"Employees_USA":
E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen
Now we want to list all the different employees in Norway and USA.
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example
above we have two employees with equal names, and only one of them will be listed. The
UNION command selects only distinct values.
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
The SELECT INTO statement is most often used to create backup copies of tables.
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_tablename
Or we can select only the columns we want into the new table:
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename
SQL SELECT INTO Example
Make a Backup Copy - Now we want to make an exact copy of the data in our "Persons" table.
SELECT *
INTO Persons_Backup
FROM Persons
We can also use the IN clause to copy the table into another database:
SELECT *
INTO Persons_Backup IN 'Backup.mdb'
FROM Persons
We can also copy only a few fields into the new table:
SELECT LastName,FirstName
INTO Persons_Backup
FROM Persons
The following SQL statement creates a "Persons_Backup" table with only the persons who lives
in the city "Sandnes":
SELECT LastName,Firstname
INTO Persons_Backup
FROM Persons
WHERE City='Sandnes'
SQL SELECT INTO - Joined Tables
Selecting data from more than one table is also possible.
The following example creates a "Persons_Order_Backup" table contains data from the two
tables "Persons" and "Orders":
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
The SELECT INTO statement is most often used to create backup copies of tables.
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_tablename
Or we can select only the columns we want into the new table:
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename
We can also use the IN clause to copy the table into another database:
SELECT *
INTO Persons_Backup IN 'Backup.mdb'
FROM Persons
We can also copy only a few fields into the new table:
SELECT LastName,FirstName
INTO Persons_Backup
FROM Persons
The following SQL statement creates a "Persons_Backup" table with only the persons who lives
in the city "Sandnes":
SELECT LastName,Firstname
INTO Persons_Backup
FROM Persons
WHERE City='Sandnes'
The following example creates a "Persons_Order_Backup" table contains data from the two
tables "Persons" and "Orders":
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
To change the data type of a column in a table, use the following syntax:
Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data
type specifies what type of data the column can hold. For a complete reference of all the data
types available in MS Access, MySQL, and SQL Server, go to our complete Data Types
reference.
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two-
digit or four-digit format.
Indexes allow the database application to find data fast; without reading the whole table.
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries.
Note: Updating a table with indexes takes more time than updating a table without (because the
indexes also need an update). So you should only create indexes on columns (and tables) that
will be frequently searched against.
Note: The syntax for creating indexes varies amongst different databases. Therefore: Check the
syntax for creating indexes in your database.
Parameter Description
Now we want to display the products and prices per today's date (with today's date displayed in
the following format "YYYY-MM-DD").
SQL Functions
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
SQL Hosting
If you want your web site to be able to store and display data from a database, your web server
should have access to a database system that uses the SQL language.
If your web server will be hosted by an Internet Service Provider (ISP), you will have to look for
SQL hosting plans.
The most common SQL hosting databases are MySQL, MS SQL Server, and MS Access.
You can have SQL databases on both Windows and Linux/UNIX operating systems.
Below is an overview of which database system that runs on which OS.
MS SQL Server
MySQL
To learn more about web hosting, please visit our Hosting tutorial.
http://www.w3schools.com/sql/sql_quiz.asp