MYSQL Lab1 RM
MYSQL Lab1 RM
/*to display id and name of students in hostel no 10 having percentage more than 90.6*/
mysql >SELECT id,name FROM students where percentage>90.6 and hostel=10;
/*to display id and name of students having percentage more than 90.6 but do not belong to hostel no 10*/
mysql >SELECT id,name FROM students where percentage>90.6 and hostel != 10;
OR
mysql >SELECT id,name FROM students where percentage>90.6 and not hostel = 10;
/*to display id and name of students having percentage between 60 and 90.6*/
mysql >SELECT id,name FROM students where percentage>=60.0 and percentage<=90.6;
1
AND and OR may be intermixed. If you do that, it's a good idea to use parentheses to indicate how
conditions should be grouped.
This is just to view the result of arithmetic operation and not to make any changes in the table. To do so we need
another DML command named update.
similarly there are inbuilt functions for arithmetic, string, numeric and date operations.
SORTING ROWS
You may have noticed in the preceding examples that the result rows are displayed in no particular
order. However, it's often easier to examine query output when the rows are sorted in some meaningful
way. To sort a result, use an ORDER BY clause.
Example://to display names of all students sorted by percentage in ascending order
mysql> SELECT name, percentage FROM students ORDER BY percentage;
To sort in reverse order, add the DESC (descending) keyword to the name of the column you
are sorting by:
mysql> SELECT name, percentage FROM students ORDER BY percentage DESC;
You can sort on multiple columns. For example, to sort by hostel no of students, then by name, use the
following query:
2
mysql> SELECT name, hostelno, percentage FROM students ORDER BY hostelno, name DESC;
The LIMIT clause is used to specify the number of records to return from the top.
The LIMIT clause can be very useful on large tables with thousands of records. Returning a large
number of records can impact on performance.
Syntax:
SELECT column_name(s)
FROM table_name
LIMIT number;
Example:
To list first 3 id nos:
mysql> SELECT name FROM students limit 3;
To find names containing exactly five characters, use the `_' pattern character:
mysql> SELECT * FROM students WHERE name LIKE "_____";
2. Min function min( ): This function gives the least of all values of the column present in the argument.
3
Example://to find minimum percentage of students
mysql> SELECT MIN(percentage) FROM students;
3. Max function max( ): This function gives the maximum of a set of values of the column present in the
argument.
Example://to find maximum percentage of students
mysql> SELECT MAX(percentage) FROM students;
4. Sum function sum( ): This function is used to obtain the sum of a range of values of a record set.
Example://to find sum of percentage of all students
mysql> SELECT SUM(percentage) FROM students;
5. Count function count( ): This function is used to count the number of non-NULL results.
Example://to count total number of students
mysql> SELECT COUNT(*) FROM students;
Foreign key- is a field in one table that refers to primary key of another table.The table containing
the foreign key is called the child table, and the table containing the candidate key is called the
referenced or parent table.
SQL Query-
CREATE TABLE Orders (