How to know how to narrow down the query - sql

I'm basically learning the SQL on my own and i have a doubt which is mentioned below.
I want to find out the particular employee's salary*12+100 from the employees table.
I have tried in many ways, which is not giving me any result.
Below example is for whole table, however i want to know for particular employee:
SELECT First_name,
salary,
12*Salary+100
FROM employees;

You really need to give us more information on your table how it is setup to give you a proper answer.
To answer your general question you are going to need to use the WHERE clause. What this does in SQL is filters out the data in the table based on the criteria you use in the WHERE clause.
For example lets pretend you have a Employe_Id Column that contains the Employee ID. You would do the following.
SELECT First_name,
salary,
12*Salary+100
FROM employees
WHERE Employe_Id = '123456';
What this would do is tell the database is pull the rows that have a value of 123456 in the column Employe_Id.
For more information on exactly what you can do I suggest you take a look at this page. Which explains it in an easy to understand fashion.

Related

Assistance with part of a join

I'm a college student and the database I'm working with is purely fictional but part of it requires me to make a query that is a join.
What I have are 3 tables each with part of the data but also needing to use two of those tables as conditions based off the main table. What I mean is I have an employee table, order table and customer table and the only thing any two of them have in common is the ID of either the employee or the customer is part of the order table.
Now what I am trying to do is create a join statement that will get certain information from the employee and customer tables and only those that both the employee and the customer are also on the same line in the order table. How should i make this type of conditional statement?
Any example using the same basic scenario will work I can use that to help me build my own query.
This is what I have right now:
SELECT [Customer/Vendor_Info_local].Name_of_customer,
Employee_Info_local.Employee_Name
FROM Employee_Info_local,
[Customer/Vendor_Info_local],
Order_Information_local
WHERE (([Customer/Vendor_Info_local].[Customer/VendorID] =
[Order_Information_local].[Cu‌​srtomer/VendorID])
AND
([Employee_Info_local].[EmployeeID] = [Order_Information_local].[EmployeeID]));
I keep getting a type mismatch error when i try to use it and honestly not even sure what that means.

SQL Select Query - Removing Duplicates/Misspelled Data

Pulling data from a cmdb into another repository. Problem is the cmdb data has misspelled/duplicate records (e.g., some assets have a Department Name as Marketing, or Markting, or Marketing&amp -- when they are all just in Marketing). Want to run a select query that displays all incorrectly named department records as the single, correct name. Any help on how to approach this?
You can use CASE in to display "marketing" for its wrong entries. But query can be complicated depending on variations.
Better + easier way is a global search and replace in column. Following article describes it:
http://www.codecandle.com/articles/sql/update/483-sql-update-with-global-search-and-replace.html
Cleaning duplicate rows, following article may help:
http://www.codecandle.com/articles/sql/windowing/503-deleting-duplicate-rows-using-windowing.html
I'm sure this is passed but http://openrefine.org/ would probably help you clean the messy data.
you can use the SELECT DISTINCT statement is used to return only distinct (different) values.
you should use distinct keyword before coloumn names in select statement.
e.g: select distinct name (Coloumn name)
from table name;

How to retrieve a column value using a variable

I have a single SQL table with columns Mary, Joe, Pat and Mick
I have rows of values for each persons weekly expenses
I want a single sproc to query a persons weekly expense value
i.e. a sproc that takes two variables #PersonsName and #WeekNumber and returns a single value
It wont work for me. It will work if I specify the persons name in the query but not if I pass the persons name to the query.
I'm pulling my hair out - is this something to do with Dynamic SQL?
Not sure if you are really digging into performance, scalability etc. as your scenario seems like a small personal tool. But still... as juergen suggested, you better design the schema in a different way. your current design won't scale easily if you want to add one more person to your database.
Instead, you can have something like
a persons table(person name, person Id)
an expenses table (week number, person id, expense)
This scales well. and with indexing on person id your queries can be faster
Suggestions apart,
Can I pass variable to select statement as column name in SQL Server seems to provide answer for your question

SQL Select If Then

The table contains numerous rows for the same person and all columns contain the same data. For Instance:
FullName Gender DOB
---------------------------------
Mary Jones Female 2012-05-01
I would like to select one row for each individual to appear in a report. I thought the easiest way might be to us an if statment to check if the next row is the same as the first unique row.
As others have said, since all the rows are the same you can use the distinct keyword:
SELECT DISTINCT <columns>
FROM table
However, you're likely to find that the reason for these duplicate records is that at least one column somewhere in the table is different. In that case, you may need to use GROUP BY instead:
SELECT <columns>
FROM table
GROUP BY <columns>
If you need to show data that is not part of the grouped columns, you only list the columns that match in the GROUP BY clause and then you have to use an aggregate function or sub query to bring data from the unique columns into the select list. GROUP BY queries can get really complex and make your head hurt.
If the columns really are consistently the same, something in your schema or application design isn't right. It's rare that you should ever have truly duplicate records in a table, and it's more likely to mean something is wrong.
Finally, I need to comment about your IF/THEN request. This idea is way off. SQL has a SET-based or declarative programming style. Thinking about problems using procedural tools like IF/THEN will only lead to trouble. Even if you really do need a conditional expression, the way to do it within an SQL statement is via a CASE expression.
If the data is the same in all columns, then you can use the DISTINCT keyword:
SELECT DISTINCT FullName, Gender, DOB
FROM yourtable
This will check that the data in all fields are unique removing any duplicates.
See SQL Fiddle with Demo
Another way to write this is using a GROUP BY on all fields:
SELECT FullName, Gender, DOB
FROM yourtable
GROUP BY FullName, Gender, DOB
You should use the distinct clause instead.. if all the columns are in fact completely same.
select distinct FullName, Gender, DOB
from <your_table_name>;
Having duplicate rows is usually a sign of something wrong (may be the data is being loaded multiple times). You might have to investigate to see the actual reason.

Retrieve data from two different table in a single report

I have two table Employee and Salary table, salary consists Salary of employee in a field named Salary_employee.
Second one is Extra Expense, Extra expense consists records related to extra expenses of a company like electricity bills,office maintenance in a field named extra_expense.
(Their is no relationship between these two table).
Finally, I just wanted to show all the expenses of company in a report, for this i need to group both the table. what to use here join or union ??.
If there is no relationship between the two tables, then this really cannot work since you dont know where the expense is supposed to tie into. You should redesign the database if possible as this sounds impossible based on your description.
UPDATE
OK, by the look of your screenshots, I am guessing that this database only stores one companies info? And not multiple?
IF that is correct, AND if all you want to do is squish the data together into one flowing report of expenses, then I would indeed suggest a UNION. A JOIN would not give you the flow you are looking for. A UNION will just smash the two outputs together into one...which I think is what you are asking for?
SELECT ext_amount AS amount, ext_date AS date_of_trans
FROM extra_expenses
UNION
SELECT sal_cash AS amount, sal_dateof_payment AS date_of_trans
FROM employee_salary
It sounds like you don't need to use group or join. Simply query both tables separately within a script and handle them both accordingly to their structure to produce a report.
Join and union are functions which you can use to extract different information on a common thing from separate tables. E.g. if you have a user whose private details are stored in one table, but their profile information is in another table. If you want to display both private details as well as profile info, you can join the two tables by the common user name in order to combine and gather all info on the user in one query.