DBMS LAB Commands
DBMS LAB Commands
Output:
Find employees whose last name (characters after the space in their name) starts with "W".
Output:
Output:
EmployeeID Initials
101 Ali
102 Bob
103 Cha
104 Dav
105 Eve
4. Substring with Case Conversion
Output:
Output:
Output:
Here the examples of queries that find values within a specific range using the BETWEEN operator
and WHERE clause. Assume we have the following Employees Table:
Sql> SELECT * FROM Employees WHERE Salary BETWEEN 45000 AND 50000;
Output:
Sql> SELECT * FROM Employees WHERE EmployeeID BETWEEN 102 AND 104;
Output:
Sql> SELECT * FROM Employees WHERE Salary NOT BETWEEN 46000 AND 50000;
Output:
Find employees who joined between March 1, 2023, and August 31, 2023.
Sql> SELECT * FROM Employees WHERE JoinDate BETWEEN '2023-03-01' AND '2023-08-31';
Output:
Sql> SELECT * FROM Employees WHERE Salary BETWEEN 47000 AND 52000;
Output:
Sql> SELECT * FROM Employees WHERE EmployeeID NOT BETWEEN 102 AND 104;
Output:
Apart from the BETWEEN keyword, there are other ways to check for values within a range or
set.
We can replace BETWEEN with a combination of >= and <= for range checking.
Output:
Output:
We can also use OR for range comparisons, although this is less efficient for larger datasets.
Find employees with a salary less than 45,000 or greater than 50,000.
Sql> SELECT * FROM Employees WHERE Salary < 45000 OR Salary > 50000;
Output:
Sql> SELECT * FROM Employees WHERE Salary NOT BETWEEN 45000 AND 50000;
Output:
Although not for numerical ranges, LIKE can be used for pattern-based range checks.
Find employees whose names start with letters between "A" and "C".
Output:
Employees Table
Output:
Output:
Output:
Output:
Output:
Query: Find employees whose names contain 'o' and order them by salary in ascending order.
Sql> SELECT * FROM Employees WHERE Name LIKE '%o%' ORDER BY Salary ASC;
Output:
Output:
Output:
Query: Find employees whose names start with 'A' or 'E' and work in the HR department.
Sql> SELECT * FROM Employees WHERE (Name LIKE 'A%' OR Name LIKE 'E%') AND
Department = 'HR';
Output:
Query: Find employees whose names have 'li' starting at the 2nd position.
Output:
Query: Find employees from the IT department, order their salaries in ascending order, and
display only the top result.
Sql> SELECT * FROM Employees WHERE Department = 'IT' ORDER BY Salary ASC
LIMIT 1;
Output:
Sql> SELECT EmployeeID, CONCAT(Name, ' - ', Department) AS FullInfo FROM Employees;
Output:
EmployeeID FullInfo
101 Alice Johnson - HR
102 Bob Smith - IT
103 Charlie Brown - IT
104 Dave Wilson - HR
105 Eve White - Marketing
Output:
EmployeeID TrimmedName
101 Alice Johnson
102 Bob Smith
103 Charlie Brown
104 Dave Wilson
105 Eve White
Query: Remove leading spaces (spaces before the name) from employee names.
Output:
EmployeeID NameWithoutLeadingSpaces
101 Alice Johnson
102 Bob Smith
103 Charlie Brown
104 Dave Wilson
105 Eve White
4. String Padding Using LPAD() and RPAD()
Query: Pad employee names with '_' from the left to make the total length 15.
Output:
EmployeeID PaddedLeft
Output:
EmployeeID UpperName
Output:
EmployeeID LowerName
Output:
EmployeeID Name
Output:
EmployeeID UpdatedDepartment
102 IT
103 IT
105 Marketing
Output:
EmployeeID Position
101 7
102 5
103 9
104 5
105 4
9. Substring Extraction Using SUBSTRING()
Output:
EmployeeID SubName
101 Alic
102 Bob
103 Char
104 Dave
105 Eve