Friday 18 December 2015

Which operator is used in query for pattern matching?

LIKE operator is used for pattern matching, and it can be used as -.
 % – Matches zero or more characters.
 _(Underscore) – Matching exactly one character.
Example -.
 MySQL
Select * from Student where studentname like ‘a%’
1
Select * from Student where studentname like ‘a%’
 Select * from Student where studentname like ‘ami_’
 1
Select * from Student where studentname like ‘ami_’

What is the command used to fetch first 5 characters of the string?

There are many ways to fetch first 5 characters of the string -.
 MySQL
Select SUBSTRING(StudentName,1,5) as studentname from student
1
Select SUBSTRING(StudentName,1,5) as studentname from student
 MySQL
Select RIGHT(Studentname,5) as studentname from student
 1
 Select RIGHT(Studentname,5) as studentname from student

How to select unique records from a table?

Select unique records from a table by using DISTINCT keyword.
 Select DISTINCT StudentID, StudentName from Student.
 1
Select DISTINCT StudentID, StudentName from Student.

How to fetch alternate records from a table?

Records can be fetched for both Odd and Even row numbers -.
 To display even numbers-.
 MySQL
 Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=0
1
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=0
 To display odd numbers-.
 MySQL
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=1
1
Select studentId from (Select rowno, studentId from student) where mod(rowno,2)=1
 from (Select rowno, studentId from student) where mod(rowno,2)=1.[/sql]

How to fetch common records from two tables?

Common records result set can be achieved by -.
 Select studentID from student. INTERSECT Select StudentID from Exam 1
Select studentID from student. INTERSECT Select StudentID from Exam

How can you create an empty table from an existing table?

Example will be -.
 Select * into studentcopy from student where 1=2
1
Select * into studentcopy from student where 1=2
 Here, we are copying student table to another table with the same structure with no rows copied.

What are aggregate and scalar functions?

Aggregate functions are used to evaluate mathematical calculation and return single values. This can be calculated from the columns in a table. Scalar functions return a single value based on the input value.
 Example -.
 Aggregate – max(), count – Calculated with respect to numeric.
 Scalar – UCASE(), NOW() – Calculated with respect to strings.